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.
Q.22Medium
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.23Medium
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.24Medium
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.25Medium
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.
Advertisement
Q.26Medium
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.27Medium
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.28Medium
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.
Q.29Medium
What is the output of:
#define ADD(x,y) (x+y)
int main() { printf("%d", ADD(5,3)*2); return 0; }
Answer: A
ADD(5,3)*2 expands to (5+3)*2 = 8*2 = 16. Parentheses around the entire macro definition protect against precedence issues.
Q.30Medium
What is the output of:
#define MIN(a,b) ((a)<(b)?(a):(b))
int main() { printf("%d", MIN(10, 5*2)); return 0; }
Answer: B
MIN(10, 5*2) expands to ((10)<(5*2)?(10):(5*2)) = ((10)<(10)?10:10) = 10. Both values are equal due to evaluation order.
Q.31Medium
Which of the following about preprocessor is TRUE?
Answer: D
Preprocessor is a separate tool that processes directives before actual compilation. It prevents recursive macro expansion to avoid infinite loops.
Q.32Medium
What is the correct way to define a multi-line macro in C?
Answer: B
Backslash (\) at the end of line continues a macro definition to the next line. This is the standard way to create multi-line macros in C.
Q.33Medium
Consider the preprocessor directive #define MAX 100. If this macro is used in multiple source files, which statement is TRUE?
Answer: A
Macros are expanded during preprocessing in each translation unit separately. If #define MAX 100 appears in multiple source files, the constant 100 gets substituted in each file independently, potentially creating code duplication. To avoid this, the macro should be defined in a header file with include guards.
Q.34Medium
What is the output of the following code?
#define SQUARE(x) x*x
int main() { int a = 5; printf("%d", SQUARE(a+1)); return 0; }
Answer: C
Without parentheses around 'x' in the macro, SQUARE(a+1) expands to a+1*a+1 = 5+1*5+1 = 5+5+1 = 11. The macro should be defined as #define SQUARE(x) ((x)*(x)) to avoid operator precedence issues.
Q.35Medium
What will be the result of executing the following?
#define PRINT(x) printf(#x)
int main() { int a = 10; PRINT(a); return 0; }
Answer: A
The '#' operator (stringification) converts the macro argument into a string literal. PRINT(a) expands to printf("a"), which prints 'a'. If it were PRINT(10), it would print '10' as a string.
Q.36Medium
What is the purpose of #line directive in C preprocessing?
Answer: B
#line directive changes the line number and filename reported by the compiler in error messages and by __LINE__ and __FILE__ macros. Syntax: #line line_number "filename"
Q.37Medium
Which of the following correctly uses #pragma once for header file protection in 2024 standards?
Answer: B
While #pragma once is technically non-standard, it is supported by virtually all modern compilers (GCC, Clang, MSVC) since 2024. It's simpler than include guards and prevents multiple inclusion more elegantly. Both approaches work, but #pragma once is increasingly preferred.
Q.38Medium
Which preprocessor directive is used to generate a compiler warning or error message at compile time?
Answer: B
#error is standard C and generates a compilation error. #pragma message is commonly supported for warnings. #warn and #warning are non-standard. These are useful for version checking and compile-time notifications.
Q.39Medium
Consider nested macro expansion:
#define A B
#define B 5
printf("%d", A);
What is printed?
Answer: B
The preprocessor expands A to B in the first pass. In the second pass, it recognizes B as a macro and expands it to 5. Nested macro expansion is allowed and the final result is 5. The preprocessor performs multiple expansion passes as needed.
Q.40Medium
Which statement about __VA_ARGS__ in variadic macros is CORRECT?
Answer: A
__VA_ARGS__ (C99 feature) represents all variable arguments after the required parameters. It can be empty if no extra arguments are provided. It must be used with '...' in the macro definition. C89/C90 don't support variadic macros.