Which of the following is NOT a preprocessor directive in C?
Answer: D
#declare is not a valid preprocessor directive. Valid directives include #define, #include, #pragma, #ifdef, #endif, #if, #else, etc.
Q.2Easy
What is the output of the following code?
#define MAX 5
int arr[MAX];
Answer: B
The preprocessor replaces MAX with 5, creating an array of size 5. This is a valid use of macro expansion.
Q.3Easy
Which preprocessor directive is used to include standard library files?
Answer: C
#include <filename> is used for standard library files (searched in standard paths). #include "filename" is used for user-defined header files (searched in current directory first).
Q.4Easy
What does the following preprocessor directive do?
#define SQUARE(x) ((x)*(x))
Answer: C
This is a function-like macro that takes parameter x and expands to ((x)*(x)). The parentheses around x prevent operator precedence issues.
Q.5Medium
What is the output of this code?
#define ADD(a,b) a+b
int result = ADD(5,3)*2;
Answer: A
The macro expands to 5+3*2 = 5+6 = 11, not 16. However, due to operator precedence, it becomes 5+3*2. Actually, it expands correctly to (5+3)*2 = 16 when evaluated.
Advertisement
Q.6Medium
What is the purpose of the #ifdef directive?
Answer: A
#ifdef checks if a macro identifier is currently defined. Code following it is compiled only if the macro is defined.
Q.7Medium
Which of the following shows the correct order of preprocessor directives in a C program?
Answer: C
Preprocessor directives can appear in any order in the source code. However, logically, #include is often used before #define and #ifdef checks.
Q.8Medium
What is the difference between #include <stdio.h> and #include "myheader.h"?
Answer: B
Angle brackets search in standard system directories. Double quotes search in the current directory first, then standard directories. This is the standard convention.
Q.9Easy
What is the output?
#define MAX(a,b) (a>b?a:b)
printf("%d", MAX(10, 5));
Answer: A
The macro expands to (10>5?10:5) which evaluates to 10 since the condition is true.
Q.10Medium
What will happen if you define the same macro twice with different values?
Answer: B
When a macro is redefined, the new definition replaces the old one. Some compilers may issue a warning about redefinition.
Q.11Easy
What is the purpose of the #undef directive?
Answer: A
#undef is used to remove the definition of a macro so it cannot be used in subsequent code.
Q.12Medium
What is the output of this code?
#define SWAP(a,b) {int temp=a; a=b; b=temp;}
int x=5, y=10;
SWAP(x,y);
printf("%d %d", x, y);
Answer: B
The SWAP macro exchanges x and y values using a temporary variable. After the swap, x becomes 10 and y becomes 5.
Q.13Medium
What does the ## operator do in a macro?
Answer: A
The ## operator (token pasting) concatenates two tokens into a single token during macro expansion.
Q.14Medium
What is the output?
#define STR(x) #x
printf("%s", STR(hello));
Answer: A
The # operator (stringification) converts the macro argument into a string literal. STR(hello) becomes "hello" as a string.
Q.15Hard
Which statement about #define is TRUE?
Answer: B
Macros are purely textual substitutions performed by the preprocessor at compile time, not at runtime. They don't allocate memory themselves.
Q.16Hard
What is the problem with this macro?
#define DOUBLE(x) x*x
int result = DOUBLE(2+3);
Answer: B
The macro expands to 2+3*2+3 = 2+6+3 = 11, not 25. This is because x is not parenthesized. Should use #define DOUBLE(x) ((x)*(x))
Q.17Hard
What will be the output?
#define MIN(a,b) ((a)<(b)?(a):(b))
int x=5; int y=10;
int z = MIN(x++, y++);
Answer: A
The macro expands to ((x++)<(y++)?(x++):(y++)). In the condition, x++ returns 5 and increments x to 6. Since 5<10 is true, x++ is evaluated again (x becomes 7, but z gets 6). Actually, this shows side effects problem - x becomes 7, y stays 10.
Q.18Hard
What is the difference between #if and #ifdef?
Answer: A
#ifdef checks if a macro is defined (exists). #if evaluates a constant integer expression (can check macro values, compare numbers, etc.).
Q.19Hard
What preprocessor features should be avoided for safer code?
Answer: B
Function-like macros can cause issues due to multiple evaluation of arguments and operator precedence problems. Modern C prefers inline functions and const for safety.
Q.20Easy
What is the primary role of the C preprocessor in the compilation process?
Answer: A
The preprocessor is a separate program that processes source code before the compiler. It handles directives like #include, #define, and #ifdef.