Basic structure of C program

This is obsolete content Click Here to View Updated Version and Much more

The format of writing C program is called Structure. It consists of following parts:
  • Preprocessor directives
  • Main function
  • Program body(C statements)

Processor directive

Preprocessor directive is an instruction given to the compiler before the execution of actual program. Preprocessor directive is also known as compiler directive. The preprocessor directives are processed by a program known as preprocessor. It is part of C++ compiler. It is a program that processes our source program before it is passed to the compiler. The semicolon is not used at the end of preprocessor directives. The preprocessor directive start with hash symbol # and the key word include or define. These directives are writer at the start of program. The directives can be placed anywhere in a program but are most often placed at the beginning of a program.

Types of processor directives

Two types of preprocessor directives used in C are as follows:
  • include preprocessor
  • define preprocessor

Include processor directive

The include preprocessor directive enables a program to include to access a library file. Each library file contains different header files. The include preprocessor directive is used to include header files in program.

Syntax :

The syntax of using include preprocessor directive is as follows:
#include<standard header file name with extension>

Example

#include<stdio.h>
#include<math.h>
The above statements tells preprocessor to include the file stdio.h and math.h in source program before compiling it. The include directive tells the compiler where to find the meanings of standard identifiers such as printf. The meanings are describes in standard header files. The header file stdio.h contains information about standard input and output functions such as scanf and printf. The math.h contains information about common mathematical functions.

Define processor directive

The define directive is used to define a constant. It can be used anywhere in a program before its use.

Syntax

The syntax of define directive is as follows:
#define identifier value
#             it indicates the start of preprocessor directive
define      it is used to define a constant
Value       it represents the value associated with the identifier.
The preprocessor directive replaces all occurrences of identifier with the value. The identifier is conventionally written in uppercase.

Example

#define PI 3.141593

Header files

Header files are collection of standard library functions to perform different tasks. There are many header files for different purposes. Each header file contains different types of predefined functions. Many header files con be included in one program. The header file must be included in the program before calling any of its function in the program. The extension of a header file is .h. The include preprocessor directive is used to include a header file in programs. These files are provided by C compiler system. The header files are normally stored in INCLUDE subdirectory. The name of header file is written in angle brackets < >.

Syntax:

The syntax of using header files is as follows:
#include<header file name with extension>
The name of header file can also be used in double quotes as follows:
#include"header file name with extension"

Example

#include"stdio.h"
The word stdio.h stands for standard input/output. The header file contains the definitions of built-in input and output functions.
A header file math.h is used in program to use predefined mathematical functions. The following statement is used to include this file in a program.
#include<math.h>

Main function

The main() function is the place where the execution of a C program starts. When the program is executed, the control enters main() function and starts executing its statements. Each program must contain main() function. If a program does not contain main() function it can be compiled but cannot be executed. Any number of statements con be written in the body of the main() function. The body is enclosed in braces { }.

Syntax:

The syntax of main() function is as follows:
Void main(void)
{
body of main function
}
The main function consist of the following:
  • The definition of main function starts with keyword void. It indicates the type of value that is returned by function. Void means that the main function will return no value.
  • Keyword void in parenthesis indicates that function does not accept any argument (value).
  • The body of main function is enclosed in braces. It consists of C language statements. The instructions are used to implement program logics.

C statements

An instruction for the computer to perform a task is called statement. The statements are written in curly brackets. Computer performs these statements one by one in the same sequence in which these instructions are written. Each statement in C is terminated with semicolon.

Example

The following example contains two statements in the body of main() function.

#include<stdio.h>
Void main()
{
Printf("C Programming");
Printf("programming makes a life intesting.");
}

Comments

Comments are the lines of program that are not executed. The compiler ignores comments and does not include them in the executable program. That is why the comments do not affect the size of executable file. Comments are used to increase the readability of the program. Comments explain the purpose of the code. The user can insert the information notes in the code. It helps in debugging and modifying the program later. Comments can be added anywhere in a code. Comments can be added in programs in two ways:

Single-line comment:

Comments on single line are added by using double slash "//". Anything written on the right side of double slash is considered as comments and is ignored during execution.

Multi-line comments:

Multi line comments are inserted to the code by placing /* at the beginning of the comment. The character */ is used to end the multi-line comments.