Structure of C program | C – Program Structure

Structure-of-c-program

Introduction:

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. Knowing the constructs of program will give us a clear picture about Language.

Here we are trying to learn Structure of C programming language. The C Language is the structured programming language. Each C program is a collection of functions. And Every Function is a collection of statements and Each function  will perform a specific task.

Here is the General Structure of C program

Structure of C program :

The above code snippet is a blueprint of a basic C Program. We try to include the basic elements.

As you can see, We have Pre-Processor Directives, Global Variables, Functions, local variables and finally Statements and comments. 

Note: All above elements are not mandatory to create a working C program. For example, We don’t need to have Global variables, They are not mandatory. 

Now, We know the basic structure of the C Program, Let’s look into the each element to learn more about it. Let’s start with the 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-processor directives are used to add Header files and other related files to the program.
  • The preprocessor Directives are used to include useful libraries to our programs.
  • The most commonly used Preprocessor Directives are #include and #define.
  • #include directive is used for Including Library files like Header files (stdio.h) or Normal files (myheader.h).
  • #define directive is used to Define Symbolic Constants and Macros.
  • Generally, we include the stdio.h header file to our programs using #include pre-processor Directive. The stdio.h headerfile contains the standard input and output files. So we are including all these I/O functions prototypes to our program so that we can perform printing values by using printf function and Reading values by using the scanf function, etc.
  • Pre-Processor Directives also removes the Comments
  • We also use Pre-Processor Directives for Conditional compilation.

📢 We have discussed about the Pre-Processor Directives in-detailed in Following article

Global Variables :

               Global Variables are one type of Variables which are available for all Functions in Programs. It means any function can access this value and can change or modify this Variable.

These variables useful, When you want to share a variable across many functions. But we also need to be careful while using the global variables, Because any function can modify global variable value, There is good chance of data corruption ( Illegal Write).

📢 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 :

The main function is the entry point into the C program. When you run a program, The main function is the one which is going to be called by the Operating system.

  • Execution of C program is Starts with main function. The Main function is the 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 can contain the local variables, statements and function calls to other user-defined functions.

Local Variables:

The Variables which are available only within the Defined Function are called Local Variables.

If we declare any variable within the main Function then it won’t be available to any other function. For example, If you create a local variable called n under the main function, Then that local variable ( n) won’t be available for the above Function1 function.

The local variables scope is only within the defined block of code. We can say the code between the two curly braces { .... } as the block of code or Code Block in C programming language.

The Local variables will be deleted or removed once the enclosed function finished execution.

📢 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.

Each and every action like Addition, Subtraction, Variable creations all falls under statements.

Function1 or C Functions:

           

As we discussed earlier, The C Program is a Collection of functions. Functions are the block of code or bunch of statements which are trying to perform a specific task.

For example, We can have a function named add. The add function can be used to add the given numbers. So similarly we can have a function to do a specific task.

Functions also increases the code re-usability, Means we can write a function once and can be used many times. All we need to do is to call the function to get our solution.

The functions increases the readability of code and helps us to debug programs efficiently. So It is recommended to use the functions in your program.

Here Function1 is one function like the Main function. As you can see the Function1 also have the local variables, Statements and etc.

Okay, Now we know the basic building blocks or Structure of the C Program, So let’s see an Example C Program to understand the structure or format of C Program better.

Structure of C Program – Example Program:

Output:

Here is the output of above program.

C Program Structure Explanation :

In above program,

The #include is pre processor Directive. We are using pre-processor directives to include Header file named stdio.h. The stdio.h headerfile contains the standard Input and Output library functions, Which helps us to print the data on to console using printf like functions and read the data from the user using the scanf like functions.

As this stdio.h contains the essential library functions, Most of the C Programs we use/write contains this pre-processor directive.

Int the next line, We have

The int g =10; is Global Variable. The global variable g is available for the all functions in our program. So we can access the global variable g in main function and also in the add function.

Then we have

The above line is called as the forward declaration of add function, Here we are calling the add function from the main function and the add function is defined after the main function, So we need to let the main function know the prototype of the add function, So that compiler will know the prototype of add function. If it feels little confusing, Then just remember that we need to write the prototype of the function if we are calling the function before the function definition. We are going to discuss about these concepts in detailed in upcoming tutorials.

Then, We have the main function.

In the main() function, We created variables a, b, and c. Then we are trying to calculate the sum of the variable a and b.

The Addition operation is performed by the add function, So we are calling the add function with values of a and b.

The add function looks like below.

The add function is taking the two variables as input and then returning the sum of the two numbers.

So we called the add() function from main and assigned the result to variable c. So now the variable c contains the sum of variable a and variable b

Then we are printing the values of the variable c and we are also printing the value of our global variable g.

You may also like:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

Leave a Reply