• C Programming Video Tutorials

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 Preprocessor Section

The C compiler comes with several library files, having ".h" as an extension. A ".h" file (called a "header file") consists of one or more predefined functions (also called "library functions") to be used in the C program.

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.

The following statement defines a constant PI −

#define PI 3.14159

Example

Once a constant is defined, it can be used in the rest of the C program.

#include <stdio.h>
#define PI 3.14159
int main(){
   int radius = 5;
   float area = PI*radius*radius;
   printf("Area: %f", area);
   return 0;
}

Output

On executing this code, you will get the following 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

Macros are generally faster in execution than the functions.

The main() Function

A C program is a collection of one or more functions. There are two types of functions in a C program: library functions and user-defined functions.

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.

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 −

warning: return type defaults to 'int'

Example

A typical example of main() function is as follows −

#include <stdio.h>
int main() {
   /* my first program in C */
   printf("Hello, World! \n");
   return 0;
}

Output

On executing this code, you will get the following output −

Hello, World! 

The Global Declaration Section

This section consists of declaration of variables to be used across all the functions in a program. Forward declarations of user-defined functions defined later in the program as well as user-defined data types are also present in the global section.

Example of global variable declaration

int total = 0;
float average = 0.0;

Example of forward declaration of a function

float area(float height, float width);

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.

Depending on the requirements, a C program may have one or more user-defined functions, which may be called from the main() function or any other user-defined function as well.

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.

Inserting comments in the code often proves to be helpful in documenting the program, and in understanding as well as debugging the programming logic and errors.

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.

int age = 20; // variable to store age

However, a valid statement can’t be given in a line that starts with "//". Hence, the following statement is erroneous:

// Variable to store age. int age=20;

Structure of the C Program

The following code shows the different sections in a C program −

/*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

On executing this code, you will get the following output −

Side= 5.50 Area=30.25
Advertisements