Sei sulla pagina 1di 15

What is Preprocessor : Program that Processes or Analyzes the source code file before Given to the Compiler is called

as Pre-Processor (preprocessor comes before compiler so complete source code will be first processed by the preprocessor and then output will be given to the compiler.) How C Processor Works | Processing of Preprocessor Directives with Flowchart following flowchart clearly explains the working of Preprocessor Directive -

Note : Preprocessor Directive

They Begin with a # which must be first non-space character on the line They do not ends with semicolon

Tasks Performed by Pre-processor Directive in C

Joins any Line with the Backslash Character into Single Line Divide Program into Set of Tokens Expand Macros Remove Comments and Replace it by Single Space

Preprocessor Merges two lines on single line. printf("Hello \ world"); Preprocessor does this printf("Hello world")

Expand Macro #define MAX 20 main() { int i = MAX; printf("%d",i); } After Preprocessor Program will be expanded as main() { int i = 20; printf("%d",i); }

Replacing Comments with space Suppose programmer have written this code main() { int i = 20; /* this is first variable */ //Print the variable //printf is used to print variable printf("%d",i); } then

Preprocessor will replace Comment with whitespace. Comments are ignored by compiler.

Preprocessor will replace this Command with White Space as it is ignored by compiler. After Replacing Comment Code will be like this -

main() { int i = 20; printf("%d",i); }

Simple Substitution Macro : #define Preprocessor in C Syntax #define macro_identifier value Note

#define Preprocessor defines a identifier and a value that is substituted for identifier each time it is encountered in the source file Generally macro-identifier is written in the Capital Letter to distinguish it from other variables. Its like a name-value Pair.

Examples : #define PI 3.142 #define TRUE 1 #define AND && #define LESSTHAN < #define MESSAGE "welcome to C"

Step 1 : program.c [Program written by Programmer] #include<stdio.h> #define LESSTHAN < int main() { int a = 30; if(a LESSTHAN 40) printf("a is Smaller"); return(0); } Step 2 : Program is processed by Pre-processor int main() { int a = 30; if(a < 40) printf("a is Smaller"); return(0); } Step 3 : Program is processed by Compiler a is Smaller

Precautions to be Taken while Writing Simple Preprocessor

Do not Write Semicolon After #define Statement.

#define MAX 20; Pre-processor Macro taking Argument : Argumented Macro (Macro having Arguments) 1. Everytime whenever the macro name is encountered , the arguments are replaced by the actual arguments from the program. 2. Macroname having Arguments is called Argumented Macro. 3. Argumented macro is as called as function macro as it looks like calling function. Advantages of Argumented macro : 1. Arguments are not Case sensitive . 2. Numeric macro Executes faster than the function Example of Macro Name: #define SQU(x) (x*x)

Example : #include<stdio.h> #define SQU(x)(x*x) int main() { int x; float y; x = SQU(3); y = SQU(3.1); printf("\nSquare of Integer : %d",x); printf("\nSquare of Float : %f",y); return(0); }

Output : Square of Integer : 9 Square of Float : 9.610000 Conclusion : 1. We can pass any Numeric Parameter eg int,float,double 2. So that Argumented macro is not case sensitive Argumented Macro used for different purpose : #define LARGE(x,y)((x)>(y)?(x):(y)) #define SQU(x)(x*x) #define COM(s1,s2)(strcmp((s1),(s2))==0

Errors Or [ Mistakes/Misuse] of C Preprocessor in C : Preprocessor processes given source code before giving it to the compiler.Preprocessor must be used with care. Some of the Common mistakes while using preprocessor are as below 1 . Macro expansion should be enclosed within parenthesis #include<stdio.h>

#include<conio.h> #define SQU(x) x*x void main() { int num ; clrscr(); num = SQU(2)/SQU(2); printf("Answer : %d",num); getch(); } Output : Answer : 4 Explanation of Program : Above expression will be expanded as SQU(x) / SQU(x) = [ x*x / x*x] = [ x*x/x*x ] = [ x*x ] = [ x^2 ] In the program the macro #define substitutes SQU(2) by just 2*2 not by 4. thus SQU(2) / SQU(2) = [ 2*2 / 2*2] = [ 2*2/2*2 ]

= [ 2*2 ] = [ 2^2 ] =[ 4 ] How to avoid this problem ? #define SQU(x)((x)*(x)) 2 .Do not Leave blank between macro-template and its argument #define SQU(x)((x)*(x))//Write like this

Do not give space between SQU and (x) , as it becomes the part of macro expansion It results into Improper result OR Compile time Error

3 . Do not Give semicolon at the end of #define statement . #define SQU(x)((x)*(x));//Do not give semicolon at the end 4 . Allot one line for each macro preprocessor #include<stdio.h> #include<conio.h>// Write on separate line Do not write like this

#include<stdio.h>#include<conio.h>

Difference between macro and function No 1 2 3 4 5 6 Speed of Execution is Faster Before Compilation macro name is replaced by macro value Useful where small code appears many time Speed of Execution is Slower During function call , Transfer of Control takes place Useful where large code appears many time Macro Macro is Preprocessed No Type Checking Code Length Increases Use of macro can lead to side effect Function Function is Compiled Type Checking is Done Code Length remains Same No side Effect

8 9

Generally Macros do not extend beyond one line Macro does not Check Compile Errors

Function can be of any number of lines Function Checks Compile Errors

Potrebbero piacerti anche