The C program structure includes details about various parts of a program such as header files, variables, functions, etc in detail.
C Program Structure Basic Format
- Header Files
- Function Declarations
- Main function
- Variable declaration
- Comment Section
- Function definitions
Sample C Program:
#include<stdio.h>
void main()
{
printf("Inspire to learn programming");
//The above line prints Inspire to learn programming in the output window
}
The first line of code includes the header file stdio.h that has the information about the built-in function printf. Whereas the main function is the function definition which has a return type void, so it does not return any value.
Header Files
The header files include the set of predefined built-in library functions. To use the built-in function we need to include the following file such as a stdio.h for printf and scanf functions. Each header files include the datatype definition, function prototype, and the C preprocessor commands.
Whereas the preprocessor commands are resolved before the compilation of normal statements by the preprocessor. The preprocessor directives begin with a # sign and some examples are
#include<stdio.h>
#include<string.h>
#include<math.h>
If there is a situation to write a complex code in many programs, the developer can define their own standard set of instructions and create their own header file as
#include"filename"
Main Function
Generally, the point where the program execution begins is the main function. The return type or datatype of the main function can be void or int type. In the case of void, it does not return any value since void means empty or void and int type returns value 0.
void main()
int main()
int main(argc, char* argv[]){}
where argc, argv are the command line arguments, argc represents a total number of arguments that we can pass to the main function and argv is the char* type variable that we can pass to the main function.
Variable Declaration
A variable is a name given to the memory location or storage space that stores a specific value. It is declared as
Datatype variablename=value;
for example int var1=10;
The datatypes are of different types, which tells the variable how much space is needed to allocate for the particular type of variable. We will learn about data types and variables in detail in future posts.
Comments Section
Basically we add comments to include a specific note about the code for future reference and the compiler does not compile the comments. The comments can be single or multi-line comments. Examples of single-line and multi-line comments are as follows
// To check the maximum value.
or
/* We enter a value and store it in the variable1 and compare
it with other values in the list*/.
In future posts, we will learn about datatype or return type, function declaration, and function definition in detail.