Chapter 11: The Preprocessor
Introduction Is a program that processes the source code before it passes through the compiler. AKA preprocessor command line or pre-processor directives Are placed in the source program before main line All begin with # Example: #include, #define, #typedef ….
Set of commonly used preprocessor #include: specifies the files to be included #define: defines macro substitution #undef: Undefines a macro #ifdef : Test for maco definition #endif: Specifies the end of #if #ifndef: Test whether a macro not defined #if: Test a complete time condition #else: Specifies alternative when #if test fails
Types Macro substitution directives File inclusion directives Compiler control directives
Macro Substitution The key word #define followed by identifier and string Eg: #define FALSE 0 # define PI 3.14 # define CAPITAL “KATHMANDU” Types: SIMPLE ARGUMENTED NESTED
SIMPLE Previous slide example are all simple No argumented Eg: 1
Macros with arguments Pass argument with macro Eg 2: Some commonly used are #define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b)) #define ABS(a,b) (((a)>0)?(a):(-(a))
Nesting of Macro #define SQUARE(X) ((X)*(X)) #define CUBE(X) (SQUARE(X)*(X))
File Inclusion We have already used it to include file Eg: #include <stdio.h> #include ”TEST.c”
Compiler control Directive