0% found this document useful (0 votes)
4 views

C_Preprocessor

The C Preprocessor modifies source code before compilation, performing functions such as macro substitutions, conditional compilation, and file inclusion. It enhances program readability, portability, and efficiency while facilitating testing and debugging. Preprocessor directives, which start with '#', include commands like #define, #include, and #if, and are essential for managing code behavior during compilation.

Uploaded by

23ec043
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C_Preprocessor

The C Preprocessor modifies source code before compilation, performing functions such as macro substitutions, conditional compilation, and file inclusion. It enhances program readability, portability, and efficiency while facilitating testing and debugging. Preprocessor directives, which start with '#', include commands like #define, #include, and #if, and are essential for managing code behavior during compilation.

Uploaded by

23ec043
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 15

C Preprocessor

Dr. Miral M. Desai


Department of EC Engineering
FTE, CSPIT, CHARUSAT
Preprocessor in C (1)
Preprocessor in C (2)
• C Preprocessor scans and modifies the source code before compilation.
• The important functions performed by C Preprocessors are:
• Simple Macro Substitutions
• Macro with Parameters
• Conditional Compilation
• File Inclusion
• Error Generation & Pragma Directives
Advantages of Preprocessors
• Enhance Readability of Program.
• Program modification becomes easy.
• Program becomes portable and efficient
• Can be used for testing and debugging.
Preprocessors Directives
• The line starting with the ‘#’ are known as Preprocessor Directives.
• All the Preprocessor Directives are executed by the Preprocessors.
• Features of Preprocessor Directives:
• Each Preprocessor Directive start with # Symbol
• There can be only one Directive on a line
• To continue a Directive on a next line, place backslash at the end of a
line.
• Directive do not end with the semicolon
• It can be place anywhere in the program
• A Directive is active from the point of its appearance till the end of
the program
Preprocessors Directives (2)
• #define
• #include
• #if
• #else
• #elif
• #endif
• #ifdef
• #ifndef
• #error
• #undef
• #line
• #pragma
Macro
// C Program to illustrate the macro
• Macros are pieces of code in a program #include <stdio.h>
that is given some name. Whenever this
name is encountered by the compiler, // macro definition
the compiler replaces the name with the #define LIMIT 5
actual piece of code.
int main()
• The ‘#define’ directive is used to define {
a macro. for (int i = 0; i < LIMIT; i++)
{
printf("%d \n", i);
}

return 0; 0
} 1
2
3
4
Macro with Arguments
// C Program to illustrate function like macros
#include <stdio.h>

// macro with parameter


#define AREA(l, b) (l * b)

int main()
{ Area of rectangle is: 50
int l1 = 10, l2 = 5, area;

area = AREA(l1, l2);

printf("Area of rectangle is: %d", area);

return 0;
}
Nesting of Macros

#include<stdio.h>

#define Area(x) x*x


#define Costpaint(x,y,z) (z*y + Area (x))

void main()
{
int A = 8, B= 6, C = 4;
printf("The area of square= %d\n", Area(A));
printf("Cost of paint= %d\n", Costpaint(A,B,C));
}
Problem with Macros (1)
#include <stdio.h>

#define MULTIPLY(a, b) a* b

int main()
{
// The macro is expanded as 2 + 3 * 3 + 5, not as 5*8
printf("%d", MULTIPLY(2 + 3, 3 + 5));
return 0; #include <stdio.h>
}
// Output: 16 // here, instead of writing a*a we write (a)*(b)
#define MULTIPLY(a, b) (a) * (b)

int main()
{
// The macro is expanded as (2 + 3) * (3 + 5), as
5*8
printf("%d", MULTIPLY(2 + 3, 3 + 5));
return 0;
}
Problem with Macros (2)

#include <stdio.h>

#define square(x) x* x

int main()
{ #include <stdio.h>
// Expanded as 36/6*6
int x = 36 / square(6); #define square(x) (x * x)
printf("%d", x);
return 0; int main()
} {
// Expanded as 36/(6*6)
int x = 36 / square(6);
printf("%d", x);
return 0;
}
Macro Vs. Function
Conditional Compilation (1)
• #ifdef macro_name
• // Code to be executed if macro_name is defined
• #ifndef macro_name
• // Code to be executed if macro_name is not defined
• #if constant_expr
• // Code to be executed if constant_expression is true
• #elif another_constant_expr
• // Code to be excuted if another_constant_expression is true
• #else
• // Code to be excuted if none of the above conditions are true
• #endif
Conditional Compilation (2)

PI is defined
Square is not defined
#undef Directive

You might also like