2023 10 17 - 18 Preprocessor
2023 10 17 - 18 Preprocessor
Pre-processor
• 1st stage of compilation, in multistage compilation.
• A preprocessor is a program that processes our source program before it is
passed to the next stage i.e. Translator
• The preprocessor works on the source code and creates “expanded source
code” i/p is ‘C’ text file, o/p also C text file
• The preprocessed code is also known as pure-c-code
• Command to compile program up to preprocessor stage is
– gcc –E filename.c –o filename.i
prog.c
Pre-processor
prog.i
Pre-processor
Features of Preprocessor :
1. Each preprocessor directives starts with '#'.
2. There should be one preprocessor directive for one line.
3. To extend preprocessor into multiple lines, then use ('\') at end.
4. The preprocessor directives doesn't end with semicolon (';').
5. Some of the preprocessor directive are
#define, #include, #if, #else, #elif, #endif, #undef,#pragma,
#ifdef, #ifndef.
Pre-processor
Operations of Preprocessor :
Following operations are performed by preprocessors
1. File Inclusions.
2. Removal of comments.
3. Macros substitutions.
4. Conditional compilation.
header file inclusion
Include the header files is in 2 ways.
#include< > #include “ “
#include< >:
Here, the pre-processor searches for header files in a predefined
path i.e. /usr/include/
#include “ “:
Here, the pre-processor searches the header file in a present
working directory, if it is not found then it searches predefined
path /usr/include/
Syntax:
#define macro_name macro_expansion
#include <stdio.h>
int main()
{
printf("line no : %d\n",__LINE__); //represents current line number
printf("file name : %s\n",__FILE__); //represents current file name
printf("time : %s\n",__TIME__); //represents compile time in "HH:MM:SS" format
printf("date : %s\n",__DATE__); //represents compile date in "MMM DD YYYY" format
printf("%d\n",(int)NULL);
printf("%d\n",EOF);
}
User defined macros without arguments
Examples: Examples:
#define TRUE 1 #define LED_AH 7
#define FALSE 0 #define LED_AL 8
#define SW1_AH 10
#define PI 3.14
#define SW2_AL 1
#define MAX 100 #define LCD_DATA P0
#define MIN 0
#define AGE 50
#define AND &&
#define OR ||
#define superloop_starts while(1)
#define PRINT_ERROR printf(“Error”);
#define NEW_LINE printf(“\n”);
User defined macros without arguments
Examples:
#define SUM(a,b) a+b
#define MAX(x,y) ((x)>(y) ? (x) : (y))
#define MIN(x,y) ((x)<(y) ? (x) : (y))
#define SQUARE(x) ((x)*(x))
#define ISUPPER(x) (x>=65 && x<=90)
#define ISLOWER(x) (x>=97 && x<=122)
#define PRINT_NEWLINES(n) {int i; for(i=0;i<n;i++) printf(“\n”); }
User defined macros with arguments
#include<stdio.h> #include<stdio.h>
#define SQUARE(x) ((x)*(x)) int SQUARE(int x)
int main() {
{
return (x*x);
int k=5,s;
}
s=SQUARE(k++);
int main()
printf(“s=%d k=%d\n”,s,k);
return 0;
{
} int k=5,s;
SQUARE(k++) … is expanded as ((k++)*(k++)) s=SQUARE(k++);
Output: printf(“s=%d k=%d\n”,s,k);
s=30 k=7 return 0;
}
Output:
s=25 k=6
Nested Macros
#include<stdio.h>
#define SQUARE(x) ((x)*(x))
#define CUBE(x) (SQUARE(x)*x)
int main()
{
int a=10;
printf(“%d %d\n”, SQUARE(a),CUBE(a));
return 0;
}
Examples:
#define PI 3.14
#define PISQUARE PI*PI
Macros
Macro vs Variable
Scope
Conditional Compilation
Compilation of a part of code based on condition
Conditions are checked during the pre-processing phase.
Directives used:
#ifdef, #ifndef, #if, #else, #elif, #endif, #undef