Study C MCQ Questions and Answers on Preprocessor Directives. Easily attend technical job interviews with these Mulitple Choice Questions.
Go through C Theory Notes on Preprocessor Directives before studying questions.
eg. #include<stdio.h>
.C file is also called Source Code file.
#define PI 3.1428
#define CVV 156 int main() { int a=10; a = a*CVV; printf("CVV=%d",a); return 0; }
During expansion of source code, a=a*CVV is replaced with a=a*156. #define is used to declare global constants.
#define KM 1.6
#define CVV 156
Yes. C program is converted to an executable file for distribution to outside world instead of sharing your original source code which may be copy righted logic.
#define ERRMSG printf("Some error."); int main() { printf("JAR."); ERRMSG; return 0; }
In place of ERRMSG corresponding macro expansion is substituted blindly.
#define ERRMSG(a) printf("Error=%d",a); int main() { ERRMSG(10); return 0; }
Inside main the statment printf("Error=%d",10) is substituted.
printf("Error=%d",10);
#define LOGIC(a,b) (a==b) int main() { if(LOGIC(5,5)) { printf("SAME "); } return 0; }
LOGIC(5,5) is replaced by (5==5) with outer paranthesis.
#define LOOP(a) while(a>0) \ {printf("%d ",a);}
#define LOOP(a) for(int i=1;i<=a;i++) \ {printf("%d ",i);} int main() { LOOP(5); return 0; }
5 in LOOP(5) will be a condition for the FOR loop starting from i=1.
#define TANK(a) a*10+2 int main() { int a = TANK(2)*2; printf("%d",a); return 0; }
TANK(2)*2 is replaced by 2*20+2*2. It gives only 24. Remember, you should have put (a*10+2) paranthesis TANK definition to get expected results.
def.c is converted into def.l file.
cpp refers to C Pre Processor.
cpp abc.c
#include<stdio.h>