Showing 81–90 of 100 questions
in Preprocessor
What is the primary role of the C preprocessor in the compilation process?
A
It processes source code before actual compilation and performs text substitutions
B
It executes the program after compilation
C
It handles memory allocation during runtime
D
It manages hardware resources
Correct Answer:
A. It processes source code before actual compilation and performs text substitutions
EXPLANATION
The preprocessor is a separate program that processes source code before the compiler. It handles directives like #include, #define, and #ifdef.
What preprocessor features should be avoided for safer code?
A
Object-like macros only
B
Function-like macros due to side effects and lack of type safety
C
#include directives
D
Conditional compilation
Correct Answer:
B. Function-like macros due to side effects and lack of type safety
EXPLANATION
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.
What is the difference between #if and #ifdef?
A
#if checks macro values, #ifdef checks if defined
B
Both are identical
C
#ifdef is deprecated
D
#if is only for integers
Correct Answer:
A. #if checks macro values, #ifdef checks if defined
EXPLANATION
#ifdef checks if a macro is defined (exists). #if evaluates a constant integer expression (can check macro values, compare numbers, etc.).
What will be the output?
#define MIN(a,b) ((a)
A
z=5, x=6, y=11
B
z=5, x=7, y=11
C
Undefined behavior
D
z=5, x=6, y=10
Correct Answer:
A. z=5, x=6, y=11
EXPLANATION
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.
What is the problem with this macro?
#define DOUBLE(x) x*x
int result = DOUBLE(2+3);
A
No problem, result is 25
B
Result is 11 due to operator precedence
C
Compilation error
D
Result is undefined
Correct Answer:
B. Result is 11 due to operator precedence
EXPLANATION
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))
Which statement about #define is TRUE?
A
Macros are type-safe like functions
B
Macros do not allocate memory at runtime
C
Macros cannot take arguments
D
Macros are evaluated at runtime
Correct Answer:
B. Macros do not allocate memory at runtime
EXPLANATION
Macros are purely textual substitutions performed by the preprocessor at compile time, not at runtime. They don't allocate memory themselves.
What is the output?
#define STR(x) #x
printf("%s", STR(hello));
A
hello
B
"hello"
C
Error: undefined variable
D
STR(hello)
EXPLANATION
The # operator (stringification) converts the macro argument into a string literal. STR(hello) becomes "hello" as a string.
What does the ## operator do in a macro?
A
Concatenates tokens into a single token
B
Creates comments
C
Defines nested macros
D
Marks preprocessor directives
Correct Answer:
A. Concatenates tokens into a single token
EXPLANATION
The ## operator (token pasting) concatenates two tokens into a single token during macro expansion.
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);
A
5 10
B
10 5
C
Compilation error
D
Runtime error
EXPLANATION
The SWAP macro exchanges x and y values using a temporary variable. After the swap, x becomes 10 and y becomes 5.
What is the purpose of the #undef directive?
A
To undefine a previously defined macro
B
To prevent macro expansion
C
To create undefined behavior
D
To test if a macro is undefined
Correct Answer:
A. To undefine a previously defined macro
EXPLANATION
#undef is used to remove the definition of a macro so it cannot be used in subsequent code.