Preprocessor Directives in C Tutorial and MCQ
Table of Contents
Learn Preprocessor Directives in C MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests. A Preprocessor in a C programming language is a Program that processes source code of a C program, expanding Macro Definitions, File Inclusions and more.
Only through Preprocessor directives, you are able to include library files in your C program. Preprocessing in C happens before compilation. Preprocessing expressions are occasionally used to replace a portion of the source code. As a result, before passing to the compiler, Total code size increases.
Preprocessor Directives in C
All preprocessor directives start with a Pound Symbol #. There are 3 types of Preprocessor Directives
- Macro Expansion
- File Inclusion
- Conditional Compilation and Run
1. Macro Expansion Preprocessor Directives
Macro is like a function or shortcut that can be used to achieve code reuse. Syntax of defining a macro is as follows.
#define NAME EXPRESSION
Example 1:
#define AREA(x) x*x int main() { int side=20; int area = AREA(side); printf("AREA=%d", area); return 0; } // AREA(side) replaced with side * side
Example 2: Proper use of Parantheses avoids unexpected results.
#define AREA(x) x*x int main() { int side=2; float total = 20 / AREA(side); printf("AREA=%d", area); return 0; } // AREA(side) replaced with side * side // 20 / side * side // (20/side) * side // 10 * 2 // 20 //What you expected ??? // 20 / (2*2) // 5 // Correction: ??? //#define AREA(x) (x*x)
2. File Inclusion C Programming Preprocessor Directives
As the name suggests, using a C Precessor directives of File Inclusion type, you are including either Library header files or User defined files. We usually include STDIO.H in our C program to use Printf and Scanf functions.
Note: Preprocessor copies all code of the included file into our C program and sends the combined single copy of code to the compiler.
C editor like TURBO C allows defining list of directories. That can be searched for files in a later time. These directories fall into two types.
- Current Directory
- Extra Specified Directories
Syntax:
#include "filename" (or) #include <filename>
There are two ways of including a file using preprocessor directive #include.
1. #include “filename”
This notation searches file in the CURRENT directory and Extra Specified Directory list.
2. #include <filename>
This notation searches files in only CURRENT directory. If the file is not available no error is raised.
Example: Including a header file
#include<math.h> int main() { float a = 10.3f; float b = sin(a); printf("SIN(%f)=%f", a, b); return 9; }
3. Conditional Compilation Preprocessor Directives
These Conditional Compilation preprocessor directives are created. And used within our C programm itself, in contrast to Macro Expansion and File Inclusion preprocessor directives. IF, ELSE, and ELSE IF statements are used to check for conditions. The C language defines directives like #ifdef, #endif, #else, #elif, and #ifndef for Conditional Compilation. These assist with implementing multiple source codes and version control management in Hardware and Software environments.
The conditional directives check for EXISTENCE of a MACRO and compiles code accordingly. Find examples below for good understanding.
define AREA printf("SUCCESS"); int main() { #ifdef AREA printf("EUROPE"); #else printf("AMAZON"); #endif return 9; }
In the above example,. If the MACRO AREA exists, then EUROPE is printed. Otherwise, AMAZON is printed.
#IFNDEF is the reverse of #IFDEF. IFNDEF works if the MACRO does not exist.
#UNDEF Preprocessor Directive
#undef is a special preprocessor directive in C language to Nullify or Remove Definition of existing Macros.
#define AREA printf("SUCCESS"); int main() { #undef AREA #ifdef AREA printf("EUROPE"); #else printf("AMAZON"); #endif return 9; } //OUTPUT //AMAZON //Macro AREA is undefined. So #ifdef fails.
[WpProQuiz 43]
Preprocessor Directives MCQ (Interview)
1) What does #include stdio.h does in c language.?
A) It includes stdio.h into existing C program.
B) #include increases the size of C program by including the specified file contents like functions, constants etc.
C) #include includes specified file before compilation.
D) All the above
Answer [=] D
2) What is the abbreviation of C STDIO in stdio.h.?
A) Standard Input Output
B) String Terminating Operations Input Output
C) Store Input Output
D) None of the above
Answer [=] A
3) Choose a correct statement about #include<stdio.h>.?
A) A file named stdio.h will searched in all directories and included if found
B) A file named stdio.h will be searched in current directory and included if found
C) A file named stdio.h will searched in current directory and pre configured list of directories in search path and included if found
D) None of the above
Answer [=] B
Explanation:
Only current directory will searched for the specified file
4) Choose a correct C statement about #include”
A) A file named stdio.h will searched in all directories and included if found
B) A file named stdio.h will searched in current directory and included if found
C) A file named stdio.h will be searched in current directory and pre configured list of directories in search path and included if found
D) None of the above
Answer [=] C
Explanation:
Current Directory + Search Path Directories
5) In Turbo C, Search Path of Directories for #Include is mentioned under the option.?
A) Include Directories
B) Exclude Directories
C) Add Directories
D) Extra Directories
Answer [=] A
Explanation:
You can specify multiple directories separated by Semicolons(;) under search path as below.
C:\turboc;C:\abc\libs;C:\def\bc\docs;
6) Choose a correct form of using C Conditional Compilation commands IF ELSE .?
A)
#IF macroname statement1; statement2; #ELSE statement3; statement4; #END
B)
#IF macroname statement1; statement2; #ELSE statement3; statement4; #ENDIF
C)
#IFDEF macroname statement1; statement2; #ELSE statement3; statement4; #ENDIF
D)
#ifdef macroname statement1; statement2; #else statement3; statement4; #endif
Answer [=] D
Explanation:
C Conditional Compilation Commands IFDEF, ELSE and ENDIF should be in lower case letters only. The TRUE block of the IF statement is executed if macroname is present or defined. If not, the FALSE block (ELSE) is carried out.
7) What is the output of C program with conditional compilation commands.?
#define CVV 156 int main() { #ifdef CVV printf("CVV YES"); #else printf("CVV NO"); #endif return 0; }
A) 156
B) printf(“CVV YES”);
C) CVV YES
D) CVV NO
Answer [=] C
Explanation:
Macro CVV is already defined.
8) What is the output of C program with conditional compilation commands.?
#define CVV 156 int main() { #ifdef cvv printf("CVV YES"); #else printf("CVV NO"); #endif return 0; }
A) printf(“CVV YES”);
B) CVV YES
C) CVV NO
D) Compiler error
Answer [=] C
Explanation:
CVV is not equal to cvv. So cvv is not defined.
9) What is the output of C program with preprocessor directives.?
int main() { #ifdef CVV printf("CVV YES"); #else #define CVV 199 #endif printf("NEW CVV=%d",CVV); return 0; }
A) CVV 199
B) printf(“CVV YES”);
C) CVV YES
D) NEW CVV=199
Answer [=] D
Explanation:
You can define a constant using Preprocessor directive even inside a main() function or any other function. So CVV is replaced by 199 everywhere it appears.
10) What is the output of C program.?
int main() { #ifndef CVV #define CVV 199 printf("CVV=%d", CVV); #else printf("CVV=%d", 188); #endif return 0; }
A) CVV=188
B) CVV=0
C) CVV=199
D) Compiler error
Answer [=] C
Explanation:
IFNDEF is executed when macroname is Not defined. IFNDEF is the opposite of IFDEF.
11) What is the output of C program with Preprocessor directives.?
void show(); int main() { #ifndef CVV #define CVV 199 #endif show(); return 0; } void show() { printf("CVV=%d",CVV); }
A) No output
B) CVV=0
C) CVV=199
D) Compiler error
Answer [=] C
Explanation:
Yes. CVV defined inside IFNDEF is available to outside functions also. So show() can display CVV.
12) What is the output of C program with preprocessor directives.?
int main() { #ifdef CVV #define CVV 199 #elif PVV printf("Inside ELIF"); #else printf("Inside ELSE"); #endif return 0; }
A) Inside ELIF
B) Inside ELSE
C) No output
D) Compiler error
Answer [=] B
Explanation:
#ELIF is similar to ELSE IF condition.
13) What is the output of C program with #undef.?
#define BIRD 5 int main() { #ifdef BIRD printf("BIRD=5."); #else printf("UNKNOWN."); #endif #undef BIRD #define BIRD 10 printf("BIRD=%d",BIRD); return 0; }
A) BIRD=5.BIRD=5
B) BIRD=10.BIRD=10
C) BIRD=5.BIRD=10
D) Compiler error
Answer [=] C
Explanation:
Yes. You can use #define any where. Using #undef removes the definition of existing macro. No compiler error if macro is not found.
14) What is the output of C program with #undef.?
int main() { #undef BIRD printf("OKAY"); return 0; }
A) OKAY
B) Compiler error
C) BIRDOKAY
D) None of the above
Answer [=] A
Explanation:
Macro BIRD is not define. So #undef tries to remove definition if any. No compiler error will come.
15) What is a Pragma in C language.?
A) A Pragma may be an instruction to build tool to process or generate comments
B) A Pragma may be an instruction to compiler to execute specific functions at specific times say startup or exit of program.
C) A pragma may be an instruction to tell compiler to ignore certain warnings.
D) All the above
Answer [=] D
Explanation:
#pragma starup myfunction1
And #pragma exit myfunction2
#pragma warn-rvl
16) What is the C Pragma directive or command to execute a particular function at startup of program.?
A) #pragma start function1
B) #pragma statup function1
C) #pragma startnow function1
D) #prama startup function1
Answer [=] B
17) What is the output of C program with Pragma.?
void show1(); void show2(); #pragma startup show1 #pragma exit show2 int main() { printf("MAIN."); } void show1() { printf("START."); } void show2() { printf("END."); }
A) MAIN.START.END.
B) START.MAIN.END
C) START.END.MAIN
D) END.START.MAIN
Answer [=] B
18) At what stage of building a C program does Pragma work.?
A) Before Compilation
B) After compilation
C) After Linking
D) None of the above
Answer [=] A
Explanation:
A Pragma works before compilation and after Preprocessing. It tells compiler to follow or ignore certain things.
19) Choose a correct implementation of C Pragma Warning.?
A) #pragma warn -par
B) #pragma warn -rch
C) #pragma warn -rvl
D) All the above
Answer [=] D
Explanation:
RVL – Return Value missing. PAR – Parameter not used. RCH – Code not reachable.
20) Choose a correct statement about C Macro.?
A) A Macro name can be in lower or upper case.
B) A Macro can be nested. It can use another macro as part of its implementation. #define CVV 2*OLDCVV. Here OLDCVV is another macro defined prior to Macro CVV.
C) Control is not passed to Macro as it is like just a dummy String(eg. CVV) Replacement technique with implementation (Macro Expansion part)
D) All the above
Answer [=] D