Ads

Structure Of C programming Language

STRUCTURE OF C LANGUAGE:


            Actually there is no particular structure for C programming language, but there are certain rules to follow, to avoid errors. Most of the programmer’s follow certain structure to avoid errors, so it will be good if we follow the same.

            Below is the structure followed by most of the programmers.

  •      Documentation Section
  •       Preprocessor Directives Section or Linking Section
  •       Global Declaration
  •       User defined Function Declaration
  •       Main Function 
                  {
                  Local Declarations
                  statement 1;                        Body of the function main
                  statement 2;
           
                  }
  •       User defined function (if required by programmer).


Structure Of C programming


DOCUMENTATION SECTION:

  •      Here a programmer can denote his name, so that people can know that he is the author of the program, he can also write a brief description about the program and indicate date and any further details according to his wish. But all these things are not compiled by the compiler.
  • The documentation section starts with token /* and ends with token */ (of course the block comment).
  • It is not necessary that you indicate documentation section for every program but it will be good, if you are writing many programs and interlinking them. We will come across this topic in functions where we will create our own header file.




PREPROCESSOR DIRECTIVES:

  •   Preprocessor directives start with # (hash) symbol. In this section we are going to include our header files.
  •  As we know a C program consists many functions. There are many predefined functions like printf(), scanf() which we are going to use in our programs. Where is the code written for such functions? Yes, it might be somewhere in the Library Functions.
  •  The declarations for printf() and scanf() functions are stored in a header file called Stdio.h. So if we are going to use printf() and scanf() functions in our program we have to include their respective header file stdio.h in linking section.


     SYNTAX:  #include <headerfile.h >;
                ( Here headerfile indicates name of the header file we are using.
Example:
#include<stdio.h>   /* Standard input/output header file*/
#include<conio.h>   /* conio.h is not found in gcc compiler*/
#include<math.h>   /* For math functions*/
#include<string.h>   /* For string functions*/

 

GLOBAL DECLARATION:

  •      In C if we declare variable in one function we can’t use them in other function. So when we enter topic FUNCTIONS we might require variables which should appear in both the functions, for this kind of situations we go for global declarations.
  •      Let me give you an example if we are writing a mathematical program and we need to use a constant like π=3.14. As we can’t go for a symbol as a variable name, instead we use Pi, here in place of π and initialize it with a value as shown below.

Example Program:    


 #include<stdio.h>
int pi 3.14;
main()
{
     area= pi*r*r;
}
func 2()
{
perimeter= 2*pi*r;
}

 

USER DEFINED FUNCTION DECLARATION:

  •             In this section we declare user defined functions if any in our program.


MAIN FUNCTION:

  •      Main function is the function where execution starts. A program without a main function yields error because compilation starts from main() function.
  • Local Declaration: Declaration of variables required in that particular function.
  • Body of function: Here lies the code or operations to be performed by the function.

EXAMPLE PROGRAM:

/* PROG NAME           : Addition
PROGRAMMER’S NAME      : Ramana
OPERATION              : This program prints the sum of two numbers.
DATE                   : 13 July 2014*/
#include<stdio.h>
main()
{  
int sum, a=5, b=6;     /*   Local Declarations*/
sum= a + b;       /*I am adding a and b*/ /*statements in body*/
printf("\nSum is %d", sum);
}


USER DEFINED FUNCTION:


  •          These functions are defined by user. It depends upon user’s wish, it is not mandatory to write a user defined function.
  •          For a beginner pre-defined functions are more than enough.



Check out the below info graphic for clear understanding of structure of C programming language 

Structure Of C programming

No comments:

Post a Comment