0% found this document useful (0 votes)
32 views19 pages

2023 10 17 - 18 Preprocessor

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)
32 views19 pages

2023 10 17 - 18 Preprocessor

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/ 19

Pre-processor

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/

Is header file inclusion must?


Macros
Macros
The #define preprocessor directive is used to define constant or
macro substitution.

Syntax:
#define macro_name macro_expansion

Macros may use in place of functions


 In small codes
 Without using recursion
Macros
Why Macros?
Looks like function, but replaces code (execution time less)
Looks like variable, behaves as constant
Pre-processors arrangement makes application executes faster
Pre-defined Macros

#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

Is macro without body allowed?


Yes. It is possible to define macro with out any macro expansion/body.
For example,
#define TEST

What is the use of macro without body?


In conditional compilation.
#ifdef
#else
#endif
#if
#elif
#ifndef
User defined macros with 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

#define SETBIT(DATA,BITPOS) (DATA |= 1<<BITPOS)


#define CLRBIT(DATA,BITPOS) (DATA &= ~(1<<BITPOS))
#define CPLBIT(DATA,BITPOS) (DATA ^= 1<<BITPOS)
#define TESTBIT (DATA,BITPOS) ((DATA>>BITPOS)&1)

Binary equivalent of given integer:


for(bitpos=31;bitpos>=0;bitpos--) int testbit(int data,int bitpos)
printf(“%d ”,(data>>bitpos)&1); {
return ((data>>bitpos)&1);
for(bitpos=31;bitpos>=0;bitpos--) }
for(bitpos=31;bitpos>=0;bitpos--)
Printf(“%d ”,TESTBIT(data,bitpos));
printf(“%d ”,testbit(data,bitpos));
Points to be noted

1. If macro is accepting arguments, it is better to put parentheses around entire


macro expansion and also around each argument.
2. No space between the macro_name and left parenthesis.
Reason: Macro expansion starts from the left parenthesis.
3. Don’t try to write recursive macros.
Reason: if macro name appears inside its own expansion, then it is not
expanded.
Ex: #define FACT(x) x==0 ? 1 : FACT(x-1)
4. If the macro_name appears inside a character constant, string constant or a
comment then it is not replaced …. Left as it is.
Ex: #define Hello 2
#define World! 3
printf(“Hello World!”);
Macro vs Function

#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

Note: Every #if directive should end with #endif


Conditional Compilation
Syntax:
#if constant-expression #if constant-expression
statements / logic statements / logic
#endif #elif constant-expression
statements / logic
#elif constant-expression
statements / logic
#if constant-expression #else
statements / logic statements / logic
#else #endif
statements / logic
#endif

You might also like