Preprocessor directives

Preprocessor directives:

In C,Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This process is cknow as preprocessing.

Commands used in preprocessor are called preprocessor directives and they start with “#” symbol.
Below is the list of preprocessor directives that C programming language offers.

Preprocessor Syntax/Description
Macro Syntax is: #define This macro defines constant value and can be any of the basic data types.
Header file inclusion Syntax is: #include The source code of the file “file_name” is included in the main program at the specified place.
Conditional compilation Syntax is: #ifdef, #endif, #if, #else, #ifndefSet of commands can content or can not cotent in source program before compilation with respect to the condition.
Other directives Syntax is: #undef, #pragma #undef can be used to undefine a defined macro variable. #Pragma is used to call a function before and after main function in a C program.
a) Program for #Define,#include prepocessor

#include <stdio.h>

#define height1 200

#define number1 3.14

#define letter1 ‘A’

#define letter_sequence1 “PQR”

#define backslash_char1 ‘\?’

void main()

{

printf(“value of height : %d \n”, height1 );

printf(“value of the no. : %f \n”, number1 );

printf(“value of letter : %c \n”, letter1 );

printf(“value of letter_sequence : %s \n”, letter_sequence1);

printf(“value of backslash_char : %c \n”, backslash_char1);

}

Output

value of height : 200

value of the no. : 3.140000

value of letter : A

value of letter_sequence : PQR

value of backslash_char : ?

b)example program for #ifdef, #else and #endif :

#include<stdio.h>

#define DENNIS 100

int main()

{

#ifdef DENNIS

printf(“DENNIS is defined. So,that this line will be added in ” \ “this C file\n”);

#else

printf(“DENNIS is not defined\n”);

#endif

return 0;

}

Output

DENNIS is defined. So, that this line will be added in this C file

c) example program for #ifndef and #endif :

#include<stdio.h>

#define DENNIS 100

int main()

{

#ifndef SELVA

{

printf(“SELVA is not defined. So, that purpose we are going to ” \ “define here\n”);

#define SELVA 800 }

#else printf(“SELVA is already defined in the program”);

#endif return 0;

}

Output

SELVA is not defined. So, that purpose we are going to define here

d) program for #if, #else and #endif :

#include<stdio.h>

#define a1 150

int main()

{

#if (a1==150)

printf(“This line was added in this C file since ” \ “a1 \= 150\n”);

#else

printf(“This line will be added in this C file since ” \ “a1 is not equal to 150\n”);

#endif

return 0;

}

Output

This line was added in this C file since a1 = 150

e)program for undef :

#include<stdio.h>

#define height1 200

void main()

{

printf(“First defined value for height : %d\n”,height1);

#undef height1 // undefining variable #define height1 700 // redefining the same for new value

printf(“value of height after undef \& redefine:%d”,height1);

}

Output

First defined value for height : 200 value of height after undef & redefine : 700

 

Leave a Reply

Your email address will not be published. Required fields are marked *