Preprocessor Directives tutorial
Table of Contents
Learn C Programming Preprocessor Directives 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. Part of the Source code is replaced with Preprocessing expressions where ever encountered. So the size of Total code increases before passing to the Compiler.
Preprocessor Directives
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 42]
Preprocessor Directives Interview Questions
1) What are the types of C Preprocessor Directives.?
A) Macros
B) Conditional Compilation
C) File Inclusion
D) All the above
Answer [=] D
2) Processor Directive in C language starts with.?
A) $ symbol (DOLLAR)
B) @ symbol (At The Rate)
C) & symbol (Ampersand)
D) # symbol (HASH)
Answer [=] D
Explanation:
eg. #include<stdio.h>
3) Preprocessor in C language works on.?
A) DOTC file (.c)
B) DOTEXE file (.exe)
C) DOTH file (.h)
D) DOTCP file (.cp)
Answer [=] A
Explanation:
.C file is also called Source Code file.
4) What is the another name for .C file.?
A) Executable code
B) Source Code
C) Distributable Code
D) Macro code
Answer [=] B
5) What is the keyword used to define a C macro.?
A) def
B) definition
C) define
D) defy
Answer [=] C
Explanation:
#define PI 3.1428
6) What is the output of C program with #define.?
#define CVV 156 int main() { int a=10; a = a*CVV; printf("CVV=%d",a); return 0; }
A) 0
B) 1560
C) 1560
D) Compiler error
Answer [=] C
Explanation:
During expansion of source code, a=a*CVV is replaced with a=a*156. #define is used to declare global constants.
7) What is the C keyword used to create global Constants.?
A) constant
B) definition
C) def
D) define
Answer [=] D
Explanation:
#define KM 1.6
8) Choose a correct C statement about #define statement.?
#define CVV 156
A) CVV is called Macro Expansion. 156 is called Macro Template.
B) CVV is called Macro Expansion. 156 is also called Macro Expansion.
C) CVV is called Macro Template. 156 is called Macro Expansion.
D) None of the above
Answer [=] C
9) What is the output file generated after processing a .C file.?
A) .h file
B) .exe file
C) .cp file
D) .bak file
Answer [=] B
Explanation:
Yes. Instead of sharing your original source code, which might contain copy-righted logic,. A C program is converte into an executable file for distribution to the outside world.
10) How do you safeguard your .C file code from copying by outside developers or world.?
A) Encrypt a C file and share
B) Obfuscate a C file and share
C) Scramble a C file and share
D) Convert to Exe and share.
Answer [=] D
11) What is the output of C program with macros.?
#define ERRMSG printf("Some error."); int main() { printf("JAR."); ERRMSG; return 0; }
A) JAR.
B) JAR.ERRMSG
C) JAR.Some error.
D) Compiler error
Answer [=] C
Explanation:
In place of ERRMSG corresponding macro expansion is substituted blindly.
12) What is the output of C program with macros.?
#define ERRMSG(a) printf("Error=%d",a); int main() { ERRMSG(10); return 0; }
A) ERRMSG(10)
B) Error=
C) Error=10
D) Compiler error
Answer [=] C
Explanation:
Inside main the statment printf(“Error=%d”,10) is substituted.
printf("Error=%d",10);
13) What is the output of C program.?
#define LOGIC(a,b) (a==b) int main() { if(LOGIC(5,5)) { printf("SAME "); } return 0; }
A) SAME
B) LOGIC(5,5)
C) 5==5
D) Compiler error
Answer [=] A
Explanation:
LOGIC(5,5) is replaced by (5==5) with outer paranthesis.
14) How do you separate a multiline macro in C language.?
A) Using * operator
B) Using % operator
C) Using \ operator
D) Using + operator
Answer [=] C
Explanation:
#define LOOP(a) while(a>0) \ {printf("%d ",a);}
15) What is the output of C program.?
#define LOOP(a) for(int i=1;i<=a;i++) \ {printf("%d ",i);} int main() { LOOP(5); return 0; }
A) 5
B) 5 5 5 5 5
C) 1 2 3 4 5
D) Compiler error
Answer [=] D
Explanation:
5 in LOOP(5) will be a condition for the FOR loop starting from i=1.
16) What is the output of C program.?
#define TANK(a) a*10+2 int main() { int a = TANK(2)*2; printf("%d",a); return 0; }
A) 44
B) 22
C) 24
D) Compiler error
Answer [=] C
Explanation:
TANK(2)*2 is replace by 2*20+2*2. It gives only 24. You should have put (a*10+2) paranthesis TANK definition to get expected results.
17) What is the file extension of expanded source code of .C file after preprocessing.?
A) .e file
B) .h file
C) .l file
D) .p file
Answer [=] C
Explanation:
def.c is converte into def.l file.
18) What is the command to preprocess a C file manually.?
A) pp abc.c
B) cpp abc.c
C) exp abc.c
D) op abc.c
Answer [=] B
Explanation:
cpp refers to C Pre Processor.
cpp abc.c
19) Choose a correct statement about C Macro.?
A) Macro template(eg. PI or function) will be replaced by Macro Expansion(3.1428) as many number of times as it appears in the C program increasing Source Code size in bytes.
B) Macros increase program speed when compared to functions.
C) Functions use less memory as the program code is written and placed only one place in source code. Macros put function code everywhere it is called again and again.
D) All the above
Answer [=] D
20) What is the C Preprocessor directive to be used to add a header file or any file to existing C program.?
A) #add
B) #present
C) #include
D) $include
Answer [=] C
Explanation:
#include<stdio.h>
For More Free online Quizzes of C Programming Click here
For More Free online Test of Aptitude Click here