Preprocessor definitions and directive in c

C preprocessor directives

All the preprocessors in c are not part of actual c programming language. When c language had introduced in the market there are different architecture of computers was present. To make the c language portable in different architectures or compilers, c language developer had introduced preprocessor. It is only instruction to compiler. All the preprocessor process before the staring of actual compilation and create an intermediate file. In the intermediate file all preprocessor statement is replaced by corresponding c code. During the compilation process that intermediate file is deleted by compiler automatically. 

To see the intermediate file in linux gcc compiler:

Suppose there c source code file test.c

Compile the c code with following options:

It will create three more files in same directory. They are:
1. test.i : Intermediate file
2.  test.o : Object file
3.  test.a : Assembly file
To see the intermediate file in Turbo c compiler:

Step 1: First create any c file let us assume test.c which contains:

#define max 10+2
int main() {
int a;
a = max * max;
return 0;
Step 2: Go to command prompt. (Open run in window XP. Then write cmd then press enter key.)

Step 3: Go to the directory where test .c has been created.

Step 4: write in the command prompt:

and press enter key (to create intermediate file)

Step 5: type test.i (to see the intermediate file)

We can observe in the intermediate file all the preprocessor statements has been replaced by actual c code. We will discuss more about intermediate file in the later chapters.

Why preprocessor in c programming language?
1. It improves the readability of program.
2. It makes easy to modify


Rajendra Singh JAdon said...

It works 4 me . thank you

Vicky said...

I tried but didn't work for me ..

I tried with the option

gcc -save-temps -c test.c

then it worked for me

Unknown said...

How many preprocessor directives in C and what they exactly defines?