The C Preprocessor
• The preprocessor is a part of C compilation process that
recognizes statements that are preceded by a pound sign (#).
• Although most C compilers have the preprocessor integrated into
it, the preprocessing is considered independent, since it works
on the source code before compilation.
• Preprocessor statements have a different syntax from that of
normal C statements, and are used for the following purposes :
– Including header files
– Conditional compilation
– Macro definitions
Including header files
• The # include directive is used to include both system and user header files.
• This directive instructs the C preprocessor to scan the included file before proceeding with the
current file.
• Following are the two ways for including a file :
#include < >
#include "userfile.c"
• The first statement searches for an include file by name systemfile in the standard system
directories .
• The second statement is used for including header files defined by a programmer in the current
directory. The preprocessor will first search for the file in the directory where the program exists,
and then if it does not find such a include file, then it looks for it in the standard system directories.
This preprocessor directive must appear in your program before any of the definitions contained in
the header file are referenced. The preprocessor searches for this file on the system and includes
its contents to the program at the point where the #include statement appears.
Conditional compilation
• The conditional directives are:
#ifdef - If this macro is defined
#ifndef - If this macro is not defined
#if - Test if a compile time condition is true
#else - The alternative for #if
#elif - #else an #if in one statement
#endif - End preprocessor conditional
• In the case of #ifdef, if the definition that follows
immediately is defined with #define, the following
code from that directive to the #endif directive will
be considered as part of the program and will be
compiled.
• With #ifndef, it's the other way round, i.e. if there
isn't a definition, the code up to
the #endif directive will be considered as part of
the program and will be compiled.
#include <stdio.h>
main()
{
#ifdef MAX
#define MIN 90
#else
#define MIN 100
#endif
printf(“MIN number : %d”, MIN);
}
Macro definitions
• Macros are used for assigning symbolic names to program constants, when these
are repetitively used within a program.
• Using macros improves readability of a program and also makes it more
portable.
• #define macro is the most commonly used macro. It's purpose is to define a
name for a value or constant.
• The #define macro makes a program extendable, meaning the value of the
definition can be changed in just one place, and this gets reflected in all the
places the value is used.
• #define macros can include expressions, or identifiers that are previously
declared in a macro. Macros can also contain parameters just like functions.
#include <stdio.h>
#define MIN 0
#define MAX 10
#define TRUE 1
#define FALSE 0
int main() {
int a;
int okay = FALSE;
while(!okay) {
printf("Input an integer between %d and %d: ", MIN, MAX);
scanf("%d", &a);
if(a>MAX) {
printf("\nToo large.\n");
}
else if(a<MIN) {
printf("\nToo small.\n");
}
else {
printf("\nThanks.\n");
okay = TRUE;
}
}
return 0;
}
#include <stdio.h>
#define max 5+2+5
main()
{
int i;
i=max*max;
printf("%d",i);
}
#include <stdio.h>
#define START main() {
#define PRINT printf("*******");
#define END }
START
PRINT
END