C - Program Structure



A typical program in C language has certain mandatory sections and a few optional sections, depending upon the program logic, complexity and readability. Normally a C 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.

Preprocessor section

The C compiler software comes with a number of library files, having .h as extension. A .h file (called as header file) consists of one or more predefined functions (also called library functions) to be used in the C program. These library functions must be loaded before using. The #include statement is used to include a header file. The #include is a preprocessor directive.

For example, printf() and scanf() functions are needed to perform console I/O operations. They are defined in stdio.h file. Hence, you invariably find #include statement at the top of a C program listing. Other important and frequently used header files are string.h, math.h, stdlib.h etc.

Other preprocessor directives are #define - 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

Area: 78.539749

You can also define a macro with #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 with #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.

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. It 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 return, its type is int, and you may omit it in 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

Hello, World! 

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

As mentioned above, 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. Thus, depending on the requirements, a C program may have one or more user defined functions, which may be called from main() function or any other user defined function as well.

Comments

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, and ignored by the compiler. Inserting comments in the code often proves to be helpful as documentation of 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;

Example

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

Side= 5.50 Area=30.25
Advertisements