C Programming From A to Z - 1

Hello D Guides readers.Today I am going to start a new main section called 'Programming Guides' in my blog.So the basic idea of starting is helping who start self studying programming.This first post is based on printf statements of C language.C language is the one of the high level programming languages and the best to understand the basics in programming.I will explain the main parts of a c programme using following source code.

//Hello World Programme
#include <stdio.h>
int main (void)
{
printf ("Hello World\n");
printf ("Welcome To D Guides\n");
return 0;
}

You may wonder what is this at first sight.Do not worry you will have a complete understanding before the end of this post.

  • //Hello World Programme - This is known as a comment.There are two methods to add comments to your programme.To put a single line comment you have to put "//" at the start of your line.To comment multiple lines you have to put '/*..........*/'.Your comment should be inside the field shown using a line of dots.These comments will not be executed by the time you run the programme,Comments are added to a programme for the readability and understanding of your programme
  • #include <stdio.h> -This is a directive to C preprocessor.'stdio.h' stands for "Standard Input Output Header File".If you have any printf or scanf statements you have to input this file like this.
  • { } - These left brace is the beginning of a function and right brace is the end of that function.The code inside these braces call as a block.
  • printf ("Hello World\n"); - This entire line is a statement.printf statement will display everything between double quotes when you run the programme.You must add a semicolon ';' at the end of every statement.

Next I will tell you where and how you type this programme.Open 'Terminal' of any Linux OS or any other OS supports C.Unfortunately you cannot do this using Command Prompt in Windows.Type the following command.

vi (filename).c

 

This filename can be any name you wish to name your file.Then you will get a view like this.To type you have to go to the insert mode.To do that press either 'i' or 'insert' on your keyboard.

First View Before The Insert Mode




View Of Insert Mode

Next you have to exit from vi editor using following steps.


  • To save and exit - Press 'esc' key and type :wq! or :x!
 

  • To exit without saving - Press 'esc' key and type :q! 

 
Next step is compile and run your programme.To compile type the following line in your terminal.
gcc (filename).c -o (executable file name)
Then to run your programme type ./(executable file name)
  
Your output will be like this.

 

There are few more things related to printf statements.To start the next line execution in a new line you have to put \n at the end of the double quotes or you can  use puts statement .(puts statement will execute next line in a new line automatically).

  • Output of printf ("Hello\nWorld\n"); will be like this.
Hello
World
To print tab space between two words add \t.

  • Output of printf ("Hello\tWorld\n"); will be like this.
Hello        World
These '\' is called as escape character and '\n' means new line, '\t' means tab space.This is another another method for print something in your programme.In here you have to use conversion specification for a string (%s).Conversion specifications will be discussed in details in future programming blog posts.
printf ("%s\n", "Hello World");
Thank you for visiting D Guides hope to educate you all through my new blog post section.Please stay with D Guides to learn more deeper into C Programming.Share among you friends and subscribe to D Guides.  

Comments

Post a Comment

Popular posts from this blog

Useful Tools For React Developers

Huawei Nova 2i

Best Tech Gadgets To Gift For Christmas 2017