Structure of C program | C – Program Structure

Spread the love
Structure of C program

The best way of learning programming language is by writing programs. before we are going to write any program we need to know the Structure of that programming Language. it will give a clear picture about Language.
Here we are trying to learn Structure of C programming language basically C is the structured programming language. C program is a collection of functions. Every Function is a collection of statements and it will do a specific task.General structure of C program is given below.

Structure of C program :

Pre-processor Directives
Global Variables
main( )
       Local Variables;
Function1( argument 1,argument 2)
       Local Variables;         // Comments

Now we are going to discuss each and every line and it’s meaning and why it is using let’s start with pre-processor Directives.

 Pre-processor Directives:

  • Pre-processor Directives is always started with Hash Symbol (#).
  • C program is are passed through pre processor before going to Compiler.
  • These pre processors directives are used to add  Header files to the program.
  • The main thing is preprocessor Directives are used to include files to our programs.
  • Commonly used Preprocessor Directives are #include and #define.
  • #include is used for Including Files Files may be Header files or Normal Files.
  • #define is used to Define Symbolic Constants and Macros.
  • Generally, we are using stdio.h header file using #include pr processor Directive for including standard input and output files to our program so that we can perform printing values by using printf function and reading values by using the scanf function.
Anyway, their are other uses also,, you can see at  Compilation Stages in C language.

Global Variables :

               Global Variables are one type of Variables which are available for all Functions in means every function can access this value and every function can change this Variable.

If we define any variable globally that means out of two functions and above of Main function it will available for main function and Function1.

Main Function :

  • Execution of C program is Starts with main function it is starting point of C program.
  • Every C program must be have at least one Function if a program has only one function that must be main function.
  • We cannot write any c program with out main function. Because program Execution starts with main and Ends with main so it is Mandatory.
  • The main function has two parts, Declaration of Local Variables and Statements.

Local Variables:

             Variables which are available only within the Defined Function are called Local Variables.
if we declare any variable within the main Function it will not access any other function that means Function1 can not access That Variable.

The Scope of local variable is within the Block(function).

Statements :

           C programs are the collection of Statements, statements is an executable part of the program it will do some Action.

Function1 :

           Here Function1 is one function like the Main function it also has similar parts like the declaration of Variables and statements.
Functions are mainly used to do a specific task. it will give readability and easy Debugging and Functions have many other Advantages.

Example Program:

Explanation :

  • In above program #include is pre processor Directive it is using to include Header file stdio.h.
  • int g =10; is Global Variable is is available for main function and add function.
  • main() is main function here we are trying to calculate sum of a and b. addition is performed by add function so i am passing a and b Values to add Function it is calculating sum and returning back to main and main is storing that value in C variable then printing the values of C and g.

you may like : 

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

Leave a Reply