C Programming

Structure of C

Before we go into details of C programming . let us understand structure if C program by writing small c programs.

Example 1

Write a program to print "Hello, welcome to SuchCodes." message

#include <stdio.h>
void main()
{
     /*...............Start of program......................*/
     printf("Hello, Welcome to SuchCodes");
    /*...............end of program........................*/
}

RESULT

Hello, Welcome to SuchCodes


We will learning C programming with Example 1. The first statement in program Example 1 is

#include <stdio.h>

shows that the header file <stdio.h> which is necessary for standard Input/Output is included in program. The I/O functions are defined in this file and hence it must be included. You will observe that this file is included in all C programs as I/O is comm for every program. The Example 1 uses the function printf() which is defined in header file   <stdio.h>

The C is modular language and every C program is written in form of functions. The main() is a special function in every C program which shows the entry point in C program. The word void written before main() means nothing. Normally, before function name, type of the return value is written. The main function does not return any value but it returns control back to operating system. This is the reason why void is written before main(). The empty pair of parentheses () after word "main" indicates that main() has no arguments. The left curly brace { shows the start of function and right curly brace } shows the end of the function. The statements of the function are written within Pair of braces { } which is known as the function body.

The first statement written in the body in the main function of the Example 1

               /*...............Start of program......................*/

represents a comment. The comments are written for increasing the readability of a program and always written within /* and */. A comment is non-executable statement and is removed from the program by compiler. The comments are very useful for understanding the programs. They are removed by the compilers and hence they do not affect the efficiency of the resultant programs. So, the comments should be used freely in programs. The last statement in body of main()

               /*...............end of program........................*/
is also a comment.

The statement written between two comments in the body of main()
               printf("Hello, Welcome to SuchCodes");
is function defined in  <stdio.h> which is called here to print the message.
               Hello, Welcome to SuchCodes

The semicolon(;) at the end of the statement shows the termination of the statement. The function printf() can also be used print message in multiple lines. For Example,

               printf("hello, \nWelcome to SuchCodes");  

                hello,
                Welcome to SuchCodes

Here, '\n' is newline character which forces message after '\n' to appear on second line. We Will study about printf() in more details later.