Structure of C Programs - Ocean of Programming

Wednesday 26 April 2017

Structure of C Programs

       In this article, we will see the structure of C programming. How to define and create a C program.
         A C program is a collection of one or more functions. The function is a collection of the statement and performs a specific task. 
          C programming language is Procedure Oriented Programming language. The other procedure-oriented programming language is VB, FORTRAN, Pascal. They follow the procedure. It follows a Top-Down approach. The importance is not given to data but to functions as well as the sequence of actions to be done. Data can move freely from function to function in the system. In C programming the program can be divided into smaller parts with the help of functions.

 

C Programming Structure

/*Comment Section*/
Preprocessor Directives 
Global Variables
main()
{
 local variables
 statements
     ......
     Body
     ......
     return 0;
}
function1()
{
     local variables;
     statements
     ........
     body
     .....
}
function2()
{
     local variables;
     statements
     ........
     body
     .....
} 

         In the above program, the part between /* and */ is called a comment section. That part is not executed at the time of execution of the program. This comments can be placed anywhere in the program it is mainly used for documentation purpose. 
          Before a C program is compiled in a compiler, the source code is processed by a program called preprocessor. Commands used for preprocessor are called preprocessor directives and they begin with “#” symbol. The commonly used preprocessor directives are #include and #define is used for including header files.
           After Preprocessor Directives we define Global Variables. Global Variables are usually defined at the top of the program after the preprocessor directives. They are outside the function. Every program has one or more functions. If the program has only one function it starts the execution of the program from main() function. 
         On another hand, the C has local variables that declared inside a function or block. Local variables are not known to functions on their own. If we have taken one variable in 1st function the same variable can be used in another function for the same or different task. e.g if we have taken int an in function for the addition we can use int a in 2nd function for subtraction purpose for different values. Local variables can be same or different for different function blocks.
         If we can define the function that is called as a user-defined function that also has local variables. They can be defined before or after the main block. The statements of the main are executed by one. The statement return ;  

Let us see a simple program  of C 

#include <stdio.h>
main()
{
        printf("Ocean of Programming\n");
        return 0;
}

The output of the above program is
In the above program
1. #include<stdio.h>-is the preprocessor directive. all lines that start with # are processed by a preprocessor which is a program invoked by the compiler.
2. main()-  In C, the execution typically begins with the first line of main(). The int is written before the main indicates return type of main().
3. { and }- In C program the { and } defines the scope of the function. When the there is any curly bracket is opened there must be closing of the curly bracket.
4. printf("Ocean of Programming\n");- printf is the standard library function for print something on the screen. At the end of printf ";" is used to indicate the operation termination. The statement is terminated.
5. return 0; : The return statement returns the value from main(). The value 0 typically means successful termination.
        Please feel free to comment if you find anything incorrect or you want to share more information about the topic discussed above.  

No comments:

Post a Comment