Structure of C-Language







Now let us combine all the above three sections/parts to make a complete C program.

=>/* Program to print hello world*/

#include<stdio.h>     /*link section*/

Int main()                   /*main function*/

{

      Printf(“Hello world”);

      Return 0;

}

Output:-  Hello world

Note:- only link and main function sections are compulsory. Documentation section is optional.

The various sections/parts of a C program are:-

1) Document section:- Gives the brief explanation of the program, author,date written etc…..

2) Link section:- Include header files with #include statements.

3) Definition section :- variables and initializes them using # define.

4) Global declaration section :- The  variables used throughout the program are declared.

5)  Function Prototype Declaration section:- User defined functions are declared here.

6)  Main Function: Main function is written here to start the program execution.

7)  User Defined Function Section: The bodies of all the user defined functions are defined here.

 

 

Let us see all these sections with example in detail:-

=>1) Documentation section  (optional):- It includes the comments about the program. The comment should be written between “/*” and “*/”. The C compiler Will never consider this for compilation process.

Syntax :- /* Comment 1

                     Comment 2

                     Comment 3*/

Example :-/* program for an OS */

=>2) Link section (optional):- Header files that are required to execute a C program are included in this section with the syntax.

Syntax :- #include<headerfile name>

Example :- #include<stdio.h>

 

=>3) Definition section :- In this section , variables are defined and values are set to these variables.

Syntax :- #define  variable  value

Example :- #define  pi  3.14

=>4) Global declaration section :- By defining a variable in this section, we can use that variable for entire program.

 

 Syntax :- datatype  variable= value;

Example :- int total =0;

Note :- after reading this if you can’t understand (or) feeling bore , don’t stop reading and learning. Achievements takes more time.

=>5) Function prototype declaration section (optional) :-  A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.

Syntax :- return-type function-name (type 1, type 2,........)

Example :- int sum (int, int)

=>6) Main function :- Every C program execution starts from main function and this function contain two major sections called declaration section and executable section.

Syntax:-  

 int main()

 {

    Declaration section;

    Executable section;

    Return 0;

}

Example :-

Int main()      /* main function*/

{

  Int x, y,total;    /*declaration section */

  X=1;

  Y=3;

Total= x+y;

Printf(“sum of 2 num=%d”,total);  /*executable section*/

return 0;

}

=>7)User defined Function definition section :- Users can define their own functions in this section which perform particular task as per the user requirement.

Syntax :-

return-type function-name (type 1,      type 2,……)

{

  Local variable declaration section;

  Definition section;

}

Example :-

Int sum(int a, int b)   /* user defined           Function*/

{

  Int s;   /* local variable declaration */

  s=a+b;        /*definition section */

  return 0;

}

 

Comments

Post a Comment

Popular posts from this blog

Introduction Structure of C-Language