Consider the following code:
#define PI 3.14
#undef PI
#define PI 3.14159
What is the value of PI after execution?
Answer: B
#undef removes the previous definition of PI. The subsequent #define redefines PI as 3.14159. This is valid C syntax and the final value of PI is 3.14159.
Q.42Medium
What happens when you use stringification operator (#) in a macro?
#define STRINGIFY(x) #x
Answer: B
The # operator (stringification) converts its argument into a string literal. For example, STRINGIFY(hello) becomes "hello". This is useful for creating string representations of identifiers.
Q.43Medium
What is the token pasting operator (##) used for in C preprocessor?
Answer: B
The ## operator (token pasting) concatenates two tokens into a single token during preprocessing. For example, #define CONCAT(a,b) a##b creates CONCAT(var,1) as var1.
Q.44Medium
Which of the following macro definitions will correctly compute the absolute value?
#define ABS(x) ((x)<0?-(x):(x))
Answer: A
The macro correctly uses the ternary operator with proper parenthesization. Each use of x is wrapped in parentheses to prevent precedence issues. This is a well-formed macro for computing absolute value.
Q.45Medium
What will be the result of this code?
#define MAX(a,b) (a>b?a:b)
int x = MAX(MAX(2,5), MAX(3,4));
Answer: A
Inner MAX calls: MAX(2,5)=5 and MAX(3,4)=4. Outer MAX call: MAX(5,4)=5. The macro correctly handles nested calls due to proper parenthesization.
Advertisement
Q.46Hard
How many times is the argument evaluated in this macro?
#define CUBE(x) ((x)*(x)*(x))
int result = CUBE(a++);
Answer: C
The macro expands to ((a++)*(a++)*(a++)), so the argument a++ is evaluated 3 times. This demonstrates a critical problem with macros - side effects are repeated multiple times, leading to unexpected behavior.
Q.47Easy
Which header file must be included to use the NULL macro?
Answer: D
NULL is defined in multiple standard headers including <stdio.h>, <stdlib.h>, <stddef.h>, <string.h>, and others. Any of these can be included to use NULL.
Q.48Hard
What is a dangling macro problem in C?
Answer: C
A dangling macro typically refers to a macro that is defined but its usage may cause issues. More accurately, it's when a macro's scope or definition is unclear, especially in conditional compilation scenarios where #undef might cause problems.
Q.49Medium
What will happen when this code is compiled?
#define SIZE 10
int arr[SIZE];
#undef SIZE
int arr2[SIZE];
Answer: A
After #undef SIZE, the macro SIZE is no longer defined. Attempting to use SIZE in int arr2[SIZE] will cause a compilation error because SIZE is not recognized by the compiler.
Q.50Easy
Which of the following correctly demonstrates the use of conditional compilation?
Answer: A
Option A uses #ifdef to check if DEBUG is defined, which is correct for conditional compilation. Option B would check if DEBUG has a non-zero value (expression-based). Options C and D have invalid syntax.
Q.51Medium
What is the purpose of predefined macros like __LINE__ and __FILE__?
Answer: B
__LINE__ expands to the current line number and __FILE__ expands to the current filename. These are predefined macros that provide compile-time information useful for debugging and error reporting.
Q.52Hard
Consider the following macro:
#define SWAP(a,b) {int temp=a; a=b; b=temp;}
What issue might occur with this macro?
Answer: D
While the macro works for all types (a and b are copied), the curly braces can cause issues when used in if-else statements without proper syntax (like missing semicolon after if). Additionally, it's limited to specific types. The safest answer is D regarding syntactic issues with placement.
Q.53Easy
What is the output of the following code?
#define PI 3.14
int main() { printf("%f", PI); return 0; }
Answer: C
PI is replaced by 3.14 during preprocessing. When printed with %f format specifier, it displays as 3.140000 (default 6 decimal places).
Q.54Medium
What is the output of:
#define SQUARE(x) x*x
int main() { int a = SQUARE(2+3); printf("%d", a); return 0; }
Answer: B
SQUARE(2+3) expands to 2+3*2+3 = 2+6+3 = 11 due to operator precedence. Proper macro should use parentheses: #define SQUARE(x) ((x)*(x))
Q.55Medium
Which of the following correctly defines a macro with multiple statements?
Answer: C
Option C using do-while(0) is the safest approach to avoid issues with semicolons in if-else statements. Option B can cause problems with control flow statements.
Q.56Easy
What does the ## operator in preprocessor do?
Answer: B
The ## operator concatenates two tokens into one. For example, #define CONCAT(a,b) a##b creates CONCAT(hello, world) → helloworld.
Q.57Medium
What is the output of:
#define STR(x) #x
int main() { printf("%s", STR(Hello)); return 0; }
Answer: A
The # operator converts the macro argument into a string literal. STR(Hello) becomes "Hello", and printf outputs Hello without quotes.
Q.58Easy
Which predefined macro gives the line number in the source file?
Answer: B
__LINE__ expands to the current line number. __FILE__ gives filename, __DATE__ gives compilation date, __STDC__ indicates C standard compliance.
Q.59Medium
What is the issue with this macro: #define MAX(a,b) a>b?a:b
int x = MAX(2, 3); int y = MAX(++i, ++j);
Answer: B
In MAX(++i, ++j), both i and j are incremented twice due to double evaluation. Parentheses help but don't solve side effects completely.
Q.60Medium
What is the purpose of #pragma pack() directive?
Answer: B
#pragma pack() controls how compiler aligns structure members in memory. #pragma pack(1) minimizes padding, while #pragma pack() without arguments resets to default.