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 .

printf("Hello \ world"). Preprocessor does this printf("Hello world") .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.

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

/* this is first variable */ //Print the variable //printf is used to print variable printf("%d". Comments are ignored by compiler.Replacing Comments with space Suppose programmer have written this code main() { int i = 20. } then   Preprocessor will replace Comment with whitespace.i). .

i). } . printf("%d". After Replacing Comment Code will be like this - main() { int i = 20.  Preprocessor will replace this Command with White Space as it is ignored by compiler.

Its like a name-value Pair.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.142 #define TRUE 1 #define AND && #define LESSTHAN < #define MESSAGE "welcome to C" . Examples : #define PI 3.

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

2. Argumented macro is as called as function macro as it looks like calling function. the arguments are replaced by the actual arguments from the program. #define MAX 20. Advantages of Argumented macro : 1. Arguments are not Case sensitive . Pre-processor Macro taking Argument : Argumented Macro (Macro having Arguments) 1. 2.Precautions to be Taken while Writing Simple Preprocessor  Do not Write Semicolon After #define Statement. Macroname having Arguments is called Argumented Macro. Everytime whenever the macro name is encountered . Numeric macro Executes faster than the function Example of Macro Name: #define SQU(x) (x*x) . 3.

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

610000 Conclusion : 1.(s2))==0 Errors Or [ Mistakes/Misuse] of C Preprocessor in C : Preprocessor processes given source code before giving it to the compiler. So that Argumented macro is not case sensitive Argumented Macro used for different purpose : #define LARGE(x.float.Output : Square of Integer : 9 Square of Float : 9.y)((x)>(y)?(x):(y)) #define SQU(x)(x*x) #define COM(s1. Some of the Common mistakes while using preprocessor are as below 1 .s2)(strcmp((s1).h> . We can pass any Numeric Parameter eg int. Macro expansion should be enclosed within parenthesis #include<stdio.double 2.Preprocessor must be used with care.

printf("Answer : %d". thus SQU(2) / SQU(2) = [ 2*2 / 2*2] = [ 2*2/2*2 ] .num). clrscr().h> #define SQU(x) x*x void main() { int num . num = SQU(2)/SQU(2). getch().#include<conio. } 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.

Allot one line for each macro preprocessor #include<stdio. 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 4 .= [ 2*2 ] = [ 2^2 ] =[ 4 ] How to avoid this problem ? #define SQU(x)((x)*(x)) 2 .h>// Write on separate line Do not write like this . #define SQU(x)((x)*(x)).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) . Do not Give semicolon at the end of #define statement .h> #include<conio.

h>#include<conio. 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 7 .#include<stdio.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 .

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 .

Sign up to vote on this title
UsefulNot useful