Preprocessor
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Objectives
In this chapter you will learn about
What is preprocessor?
Include preprocessor directive
Defining symbolic constants using Macros
Macros with Arguments
Predefined Macros
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Introduction
§ Preprocessing
– Occurs before a program is compiled
– Inclusion of other files
– Definition of symbolic constants and macros
– Conditional compilation of program code
– Conditional execution of preprocessor directives
§ Format of preprocessor directives
– Lines begin with #
– Only whitespace characters before directives on a
line
– Do not end with ;
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Comment Removal
int main()
{ Compile the program using
int num1; $cc –E filename.c
int num2;
int res;
// Read two number
printf("Enter two numbers : ");
scanf(" %d %d",&num1,&num2);
// Add two numbers
res = num1 + num2;
// Print the sum
printf(" Sum = %d",res);
return 0; // Successful Completion of
program
}
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
#include Preprocessor Directive
• #include directive
– Copy of a specified file included in place of the directive
– Two Forms
• #include <filename>
– Searches standard directory for file
– Use for standard header files
• #include "filename"
– Searches current directory, then standard directory
– Use for user-defined header files
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
The #include Preprocessor Directive
• Usage
– Loading header files
• #include <stdio.h>
– Programs with multiple source files
– Header file
• Has common declarations
• structures, enumerations, function prototypes
• Extract commonality of multiple program files
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
The #define Preprocessor Directive
• #define : Symbolic Constants
– Used to give a meaningful name to a constant in your program
– Format
– #define identifier replacement-text
– Example:
– #define PI 3.14159
– When program compiled, all occurrences of identifier
replaced with replacement text
– The identifier is replaced only when it forms a token.
– identifier is not replaced if it appears in a comment,
within a string, or as part of a longer identifier.
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
#define PI 3.14f #define PI 3.14f
main() main()
{ {
printf(“ PI “); PIXEL
} }
• Output • Preprocessed Output
PI PIXEL
not
3.14XEL
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Good Programming Practice
• By convention, symbolic constants
are defined using only uppercase
letters and underscores.
• Using meaningful names for symbolic
constants helps make programs more
self-documenting.
9
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
The #define Preprocessor Directive
• Advantages
– Takes no memory
– If the constant is used in several places, and if
it is #defined you need to make a change only
in #define directive. Instead of manually
changing each occurrence of the constant
• Disadvantages
– Name not be seen by debugger (only replacement
text)
– Do not have specific data type
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Macros with arguments
• Macro
– Operation defined in #define
– A macro without arguments is treated like a symbolic constant
– A macro with arguments has its arguments substituted for
replacement text, when the macro is expanded
– Performs a text substitution – no data type checking
– These are also called function-like macros
– The macro
#define SQR( x ) x * x
would cause
res = SQR( 4 );
to become
res = 4 * 4 ;
11
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
#define SQR(x) x*x
int main()
{
printf(“ %d “,SQR(5));
}
#define SQR(x) x*x
int main()
{
printf(“ %d “,SQR(5 + 2));
}
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
The macro
#define SQR( x ) x * x
would cause
SQR(5 + 2)
to become
5 + 2 * 5 + 2
And the result is 17
Use parentheses
#define SQR( x ) ((x) * (x))
would cause
SQR(5 + 2)
to become
((5 + 2) * (5 + 2))
And the result is 49
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
• There should not be a space between macro template and its
arguments
#define SQR (x) ((x) * (x))
would cause
res = SQR(5);
to become
res = (x) ((x) * (x)) (5);
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Macros with arguments
Write a Macro to accept the radius and calculate the area of
a Circle
15
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Macros with arguments
• Multiple arguments
#define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) )
would cause
rectArea = RECTANGLE_AREA( a , b );
to become
rectArea = ( ( a ) * ( b ) );
• Write a Macro to accept two arguments and find the
maximum
16
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Multiple lines in Macros
• A macro can be extended over as many lines as
required using a \ at the end of each line. The macro
ends after the first line which does not end in a
backslash
• #define MAX3(a,b,c) (a>b)?(a > c ? a : c) :(b > c ? b: c)
can be written as
#define MAX3(a,b,c) (a>b)? \
(a > c ? a : c) : \
(b > c ? b: c)
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Common Programming Error
• Forgetting to enclose macro arguments in
parentheses in the replacement text can lead
to logic errors.
18
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Performance Tip
• Macros can sometimes be used to
replace a function call with inline code
prior to execution time. This eliminates
the overhead of a function call.
19
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
#undef Preprocessor Directive
• #undef
– Removes (undefines) a name previously created
with #define
#undef identifier
– To remove a macro definition using #undef, give
only the macro identifier, do not give a parameter
list
– useful when we want to restrict the definition only
to a particular part of the program
20
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]
Predefined Macros
• Several simple macros are predefined.
• You can use them without giving definitions for them.
Macro Description
The name of the current source file. __FILE__
__ FILE __ expands to a string
__ LINE __ The line number in the current source file. The line
number is a decimal integer constant..
__ DATE __ The compilation date of the current source file. .
__TIME __ The most recent compilation time of the current
source file.
Some predefined symbolic constants.
Copyright Cranes Software International Limited. Names of products, solutions and services mentioned in this presentation are the properties of their respective owners. [Link]