C - Program Structure
C - Program Structure
C - Program Structure
A typical program in C language has certain mandatory sections and a few optional
sections, depending on the program's logic, complexity, and readability. Normally a C
program starts with one or more preprocessor directives (#include statements) and
must have a main() function that serves as the entry point of the program. In
addition, there may be global declarations of variables and functions, macros, other
user-defined functions, etc.
The library functions must be loaded in any C program. The "#include" statement
is used to include a header file. It is a "preprocessor directive".
For example, printf() and scanf() functions are needed to perform console I/O
operations. They are defined in the stdio.h file. Hence, you invariably find #include
<stdio.h> statement at the top of any C program. Other important and frequently
used header files include string.h, math.h, stdlib.h, etc.
There are other preprocessor directives such as #define which is used to define
constants and macros and #ifdef for conditional definitions.
#define PI 3.14159
Example
#include <stdio.h>
#define PI 3.14159
int main(){
int radius = 5;
float area = PI*radius*radius;
printf("Area: %f", area);
https://www.tutorialspoint.com/cprogramming/c_program_structure.htm 1/5
6/16/24, 10:56 AM C - Program Structure
return 0;
}
Output
Area: 78.539749
You can also define a macro with the "#define" directive. It is similar to a function
in C. We can pass one or more arguments to the macro name and perform the
actions in the code segment.
The following code defines AREA macro using the #define statement −
Example
#include <stdio.h>
#define PI 3.14159
#define AREA(r) (PI*r*r)
int main(){
int radius = 5;
float area = AREA(radius);
printf("Area: %f", area);
return 0;
}
Output
Area: 78.539749
There must be at least one user-defined function in a C program, whose name must
be main(). The main() function serves as the entry point of the program. When the
program is run, the compiler looks for the main() function.
https://www.tutorialspoint.com/cprogramming/c_program_structure.htm 2/5
6/16/24, 10:56 AM C - Program Structure
The main() function contains one or more statements. By default, each statement
must end with a semicolon. The statement may include variable declarations,
decision control or loop constructs or call to a library or another user-defined
function.
In C, a function must have a data type. The data type of return value must match
with the data type of the function. By default, a function in C is of int type. Hence, if
a function doesn’t have a return statement, its type is int, and you may omit it in
the function definition, but the compiler issues a warning −
Example
#include <stdio.h>
int main() {
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
Output
Hello, World!
int total = 0;
float average = 0.0;
https://www.tutorialspoint.com/cprogramming/c_program_structure.htm 3/5
6/16/24, 10:56 AM C - Program Structure
Subroutines in a C Program
There may be more than one user-defined functions in a C program. Programming
best practices require that the programming logic be broken down to independent
and reusable functions in a structured manner.
Comments in a C Program
Apart from the programming elements of a C program such as variables, structures,
loops, functions, etc., the code may have a certain text inside "/* .. */" recognized
as comments. Such comments are ignored by the compiler.
If the /* symbol doesn’t have a matching */ symbol, the compiler reports an error:
"Unterminated comment".
A text between /* and */ is called as C-style comment, and is used to insert multi-
line comments.
/*
Program to display Hello World
Author: Tutorialspoint
Built with codeBlocks
*/
A single line comment starts with a double forward-slash (//) and ends with a new
line. It may appear after a valid C statement also.
However, a valid statement can’t be given in a line that starts with "//". Hence, the
following statement is erroneous:
https://www.tutorialspoint.com/cprogramming/c_program_structure.htm 4/5
6/16/24, 10:56 AM C - Program Structure
/*Headers*/
#include <stdio.h>
#include <math.h>
/*forward declaration*/
float area_of_square(float);
/*main function*/
int main() {
/* my first program in C */
float side = 5.50;
float area = area_of_square(side);
printf ("Side=%5.2f Area=%5.2f", side, area);
return 0;
}
/*subroutine*/
float area_of_square(float side){
float area = pow(side,2);
return area;
}
Output
https://www.tutorialspoint.com/cprogramming/c_program_structure.htm 5/5