0% found this document useful (0 votes)
3 views3 pages

Lecture 18

Lecture 18 covers preprocessor directives in C programming, which include macro substitution, conditional compilation, and file inclusion. Key directives such as #define, #ifdef, and #include are explained with examples demonstrating their usage. The lecture also illustrates how to create macros with arguments and perform conditional compilation based on defined values.

Uploaded by

sanjida05alfa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Lecture 18

Lecture 18 covers preprocessor directives in C programming, which include macro substitution, conditional compilation, and file inclusion. Key directives such as #define, #ifdef, and #include are explained with examples demonstrating their usage. The lecture also illustrates how to create macros with arguments and perform conditional compilation based on defined values.

Uploaded by

sanjida05alfa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSE 1011 Lecture 18

Lecture – 18

Preprocessor directory: The source code for C program can include various
instructions to the compiler. A preprocessor directive performs macro substitution
conditional compilation and inclusion of named files.

The C preprocessor as defined by the ANSI C standard contains the following directives :
#if, #elif, #else, #ifdef, #ifndef, #endif, #include, #define, #undef, #line, #error, #pragma.

All the preprocessors begin with a #.

#define: The #define directive is used to define an identifier and a character sequence that is
encountered in the source file. The identifier is called a macro name, and the replacement
process is called macro substitution. The general form of the directive is:

#define macroname replacement_text

Example 1:
#define MAX 100
#define one 1
#define two one+one
#define three two+one
#define str “hellow”

The #define has another powerful feature. The macro name can have arguments.

Example 2:
#define MIN(a,b) ((a)<(b))?a : b
Minimum 10

void main(void)
{
int x = 10, y = 20 ; printf(“Minimum %d\n”, MIN(x,y));
}

In compile time, printf(“Maximum %d\n”, MIN(x,y)); will be


replaced by printf(“Minimum %d\n”, ((x)<(y))?x : y);

1/3
Example 3:

#define EVEN(a) a%2 = = 0 ? 1 : 0


This is odd
void main(void)
{
if(EVEN(9+1))
printf(“This is even\n”);
else
printf(“This is odd\n”);
}

Other Examples:[defining if….else & loop in marco]

#define SUM_A(x, y) \
({ \
double answer; \
if ((x) == 0 || (y) == 0) \
answer = 0; \
else \
answer = x+y; \
(answer); \
})
…………………………………………………………………………………………………………….

#define LOOP(start, end) \


for (int i = (start); i < (end); i++) { \
printf("%d\n", i); \
}

…………………………………………………………………………………………………………….

#define loopSum(start, end) \


({ int sum=0; \
for(int i=start; i<=end;i++) \
sum+=i; \
(sum); \
})

…………………………………………………………………………………………………………….

2/3
Conditional compilation: Syntax of conditional compilation is given below:

#if expression
statement
#endif

Example 4:

#define MAX 12
#define SERIAL 5
void main(void)
{
int flag = 0 ;
#if MAX>10
flag = 1;
198
#if SERIAL
int port = 198;
#else
int port = 200;
#endif
#else
char a[100]=”hellow”;
#endif

if(flag)
printf(“%d”,port);
else
printf(“%s”,a);
}

Example 5:
#define TED
TED is defined
void main(void)
{
#ifdef TED
printf(“TED is defined\n”);
#else
printf(“TED is not defined\n”);
#endif
}

3/3

You might also like