0% found this document useful (0 votes)
5 views15 pages

5.4 Macros

The document provides an overview of the preprocessor in programming, detailing its function as a program that processes code before compilation and operates under preprocessor directives. It categorizes directives into file inclusion, macro substitution, and compiler control directives, explaining their usage and syntax. Examples illustrate how to include files, define macros, and implement conditional compilation for portable programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

5.4 Macros

The document provides an overview of the preprocessor in programming, detailing its function as a program that processes code before compilation and operates under preprocessor directives. It categorizes directives into file inclusion, macro substitution, and compiler control directives, explaining their usage and syntax. Examples illustrate how to include files, define macros, and implement conditional compilation for portable programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Manish Tiwari, Department of Computer Science & Engineering

The Preprocessor

Manish Tiwari, Department of Computer Science & Engineering


Preprocessor
Preprocessor:
 A program that processes the program before compilation
 Operates under the control of preprocessor directives
 Placed before the main function
 All directives begin with the symbol #
 Semicolon in the end not required
 Divided into three categories:
1. file inclusion directives (#include directive)
2. macro substitution directives(#define directivie)
3. compiler control directives or conditional compilation
(#ifdef, #ifndef…)

Manish Tiwari, Department of Computer Science & Engineering


1. file inclusion directives
( #include<….> )
 #include directive:
The preprocessor inserts the entire contents of file into the
source code of the program in place of directive.

– Two forms
• #include <filename>
– For standard library header files
– Searches pre-designated directories
• #include “filename”
-an external file containing functions or macro definitions
can be included as a part of program
– Searches in current directory
– Normally used for programmer-defined files

Manish Tiwari, Department of Computer Science & Engineering


2. macro substitution directives
(#define preprocessor directives)
#define
 Used to define symbolic constants
•Constants represented as symbols
•When program compiled, all occurrences replaced
•Format: #define identifier string
•Replace where ever identifier is present by string
#define id 5
#define pi 3.14159
Where ever id is present in program, that will be replaced by 5
Where ever pi is present in program, that will be replaced by 3.14159

Note: Everything to right of identifier replaces:


#define pi =3.14159 // Replaces pi with "=3.14159”

Manish Tiwari, Department of Computer Science & Engineering


2. macro substitution directives
(#define preprocessor directives)

 Macro
– Operation specified in #define
– Macro without arguments #define id 5
• Treated like a symbolic constant #define pi 3.14159
– Macro with arguments
• Arguments substituted for replacement text
• Macro expanded
– Performs a text substitution

Manish Tiwari, Department of Computer Science & Engineering


2. macro substitution directives
(#define preprocessor directives)

Macro with arguments


 Example
#define PI 3.14159 Use parentheses
#define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) )
Without them,
Now if we use expression #define CIRCLE_AREA( x ) PI * x * x
area = CIRCLE_AREA( c + 2 );
area = CIRCLE_AREA( 4 );
becomes
it becomes area = 3.14159 * c + 2 * c + 2;
area = ( 3.14159 * ( 4 ) * ( 4 ) ); which evaluates
incorrectly

Manish Tiwari, Department of Computer Science & Engineering


2. macro substitution directives
(#define preprocessor directives)

 Multiple arguments:
#define RECTANGLE_AREA( x, y ) (( x ) * ( y ))
// multiline
rectArea = RECTANGLE_AREA( a + 4, b + 7 ); #define PRINT(x,y) if(x>y)\
becomes prirntf(“x>y”);\
rectArea = (( a + 4 ) * ( b + 7 ));
else\
printf(“x<=y”);
Nested macro:
#define M 5
#define N M+1
#define square(x) ((x)*(x))
#define qube(x) (square(x)*(x))

Manish Tiwari, Department of Computer Science & Engineering


3. Compiler control directives
(Conditional Compilation)
 It refers to the conditional definition of a macro.
 It ensures that the macro TEST is always defined.

For ex: define.h is suppose to has TEST


#include “define.h”
#ifndef TEST If TEST is not
#define TEST 5 defined
#endif then Define TEST

In case TEST is defined in define.h then ifndef condition


becomes false, and the directive #define TEST 5 is
ignored.

Manish Tiwari, Department of Computer Science & Engineering


3. Compiler control directives
(Conditional Compilation)
 It refers to the conditional definition of a macro.
 Similar is the case when we want a macro never to be defined
For ex:
#include “define.h”
#ifdef TEST
#undef TEST
#endif

 This ensures that even if TEST is defined in header file, the


definition is removed.

Manish Tiwari, Department of Computer Science & Engineering


3. Compiler control directives
(Conditional Compilation)

………
To make portable program
……..
#ifdef TEST
……
…….
……
#else
…….
……..
#endif

Manish Tiwari, Department of Computer Science & Engineering


3. Compiler control directives
(Conditional Compilation)
----------
---------- To make portable program
#ifdef TEST
printf(“………”);
for……….
….
…….
#endif
……..
………

 The statements between the directives #ifdef and #endif will be compiled only if
the macro TEST is defined.

Manish Tiwari, Department of Computer Science & Engineering


#if and #elif Directives
The #if directive can be used to test whether an expression evaluates
to a nonzero value or not. If the result of the expression is nonzero,
then subsequent lines upto a #else, #elif or #endif are compiled,
otherwise they are skipped.
A simple example of #if directive is shown below:
main( )
{
#if TEST <= 5
statement 1 ; If the expression, TEST <= 5 evaluates to
statement 2 ; true then statements 1 and 2 are
#else compiled otherwise statements 4 and 5
statement 4 ; are compiled.
statement 5 ;
#endif
}
Manish Tiwari, Department of Computer Science & Engineering
Nested conditional compilation: An example that uses such
directives is shown below.

#if ADAPTER == VGA


code for video graphics array
#else
#if ADAPTER == SVGA
code for super video graphics array
#else
code for extended graphics adapter
#endif
#endif

Manish Tiwari, Department of Computer Science & Engineering


Nested conditional compilation:
use of #elif
#if ADAPTER == VGA
code for video graphics array
#elif ADAPTER == SVGA
code for super video graphics array
#else
code for extended graphics adapter
#endif

Observe that by using the #elif


directives the number of #endifs
used in the program get reduced.

Manish Tiwari, Department of Computer Science & Engineering

You might also like