Writing and implementing the code

In our previous post we learned about the usage of C and environment setup for C. In this Post we will learn how to code in C.

  • Before writing the code, we have to know the C program parts. A C program consists of following parts :
    • Preprocessor commands
    • Functions
    • Variables
    • Statements & Expressions
    • Comments
  • Let us start coding by basic “HelloWorld” C program

#include<stdio.h>
void main()
{
/*Code Janthik*/
system("cls");
printf("HelloWorld");
printf("\n Welcome to Code Janthik");
getch();
}

  • From the above C program, first line of our program “#include<stdio.h>” is a preprocessor command, which tells a C compiler to include stdio.h file before going to an actual compilation.
  • The next line  “void main()” is the main function where the program execution begins. Here “void” means it doesn’t return any value.
  • For every “void main()” function we use “{“ open paranthesis which is followed by a “}”closed paranthesis. In the middle of these paranthesis we write our code.
  • In next line we used “/*…………..*/ “ this will be ignored by the compiler because we call those lines as “comments”. We can use them in any place of our code for better understanding the code. It’s our choice to use it or not.
  • The next line “system(“cls”);” is to directly clear the screen in our GNU C, where as in Turbo C we use “clrscr();” function.
  • The next line “printf(“…..”);” is a function in C which helps to display our text “HelloWorld” on  the screen.
  • We used “\n” in this “printf(“…”);” function, it is used to print the text “Welcome to Code Janthik” in next new line at output.
  • The next line “getch();” is a function to display the output on the screen.

In our next post we discuss about the tokens in C.