Preprocessor Directives in C | Basic IT Topic

Preprocessor Directives in C

Preprocessor Directives in C

Preprocessor Directives in C:-

The c preprocessor is known as CPP is a macro processor that is used automatic call by the compiler to transform your program before compilation.

Macro is a symbolic name of expression.

Different type of pre-processor directive:-

i)macro expansion: #define, #undef

ii)File inclusion directive: #include

iii)Conditional directive: #if,#else,#endif,#elif, #ifdef, #ifndef

iv)Miscellaneous directive: #pragma

All the pre-processor directives begins with # symbol.

Program:-

#define MAX 10+2

main()

{

        int k=MAX/2;

        printf("%d",k);

}

O/P-11

 

Program

#define MAX 10

main()

{

        printf("%d",MAX);

        printf("%d",MAX);

}

O/P-1010

When macro takes parameter , that is called parameterized macro or macro call.

The parameterize macro is known as the macro call.

Startup pragma is executed very first before control goes to main and exit pragma is executed at last once control leaves main

The C preprocessor CPP only process source file such as .C .cpp but does not process object and executable file

The file inclusion preprocessor directive includes file using “ “ or < >  where “ “ search the file from the current directory path and std. include path but < > searches the file only from the std.include path.

Operator uses in preprocessor:- 

# is used as string forming ## is used as token pasting operator.

File inclusion #include:-

The main objective of #include is copy the contains of the file and insert into the source file.

Test.c

main()
{
        int i;

        #include "letter.txt";

        printf("BYE\n");
}

letter.txt
for(i=1;i<=5;i++)

O/P:-

BYE
BYE
BYE
BYE
BYE

 

Difference between macro call and function call .

The function call is not a preprocess but macro call is preprocess.

Conditional Directives:-

These are the conditional directives:-  #if,#else,#endif,#elif, #ifdef, #ifndef

Display the name of source file

main()
{
        printf("%s",_FILE_);   
}

Check the headerfile include or not

#include "stdio.h"

main()

{

        #ifdef _STDIO_H

        printf("yes");

        #else

        printf("no");

        #endif;

}
O/P
yes
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community