C Program Structure
Free IDEs for C Development
In this tutorial, we will understand the C Program Structure. The main parts of a C program are as follows:
- CommentsÂ
- Preprocessor Directives
- Global Variables Declaration
- Function Prototypes
- Â main() function
- Local Variables Declaration
- Program Code
- Function definitions
Comments
Comments provide useful explanations and documentation for the readers. Comments are not executable statements in the C program. They are ignored by the compiler.
Preprocessor Directives
The preprocessor directive is a directive to the C preprocessor. It is processed before compiling the C program.
Global declaration
The global declaration section consists of the declaration of global variables. Global variables have the lifetime of the entire C program and are visible to the entire C program.
Function Prototypes
A C program can contain more than one function. If any functions are used other than the main function, we can declare the prototypes of the functions in this section.
main Function
The main function is the entry point of the C program. The C program execution starts with the main() function. The main function consists of local variable declarations and the programming statements section. The statements perform the actual program logic and can also contain function calls.
int main()
{
/*
Local declarations go here
*/
…
// Programming Statements – Program Logic
…
return 0;
} // end main
User-defined functions
The function definitions section contains the user-defined function code. This section contains the code for the functions declared in the function prototype section.
C Program Structure
Example C program to show the structure.
/** ********************************** * Program Description: * C Program Structure Demo * Filename : program.c * Author : TestingDocs * Date : 02/01/2017 * C Tutorials - www.TestingDocs.com ************************************* */ // Preprocessor directives #include<stdio.h> // Global declarations #define PI 3.1415 // Function prototypes double computeArea(double r); // main function int main() { /* Local declarations go here */ double radius = 0.0; double circleArea = 0.0; // programming statements - logic printf("Enter radius of circle ( in meters) ="); scanf("%lf",&radius); circleArea = computeArea(radius); printf("Area of circle = %lf m^2",circleArea); return 0; } // end main /* User defined function definition */ double computeArea(double r) { return PI*r*r; }
—
C Tutorials
C Tutorials on this website can be found at:
https://www.testingdocs.com/c-language-tutorial/
The IDE used in the tutorial is Code:: Blocks. To download and install Code Blocks follow the link:
https://www.testingdocs.com/download-and-install-codeblocks/
For more information on Code Blocks IDE, visit the official website of Code blocks IDE: