Which header file contains the definition of NULL macro?
Answer: C
NULL is defined in multiple headers including <stdio.h>, <stdlib.h>, <stddef.h>, and others. Any of these can be included to use NULL.
Q.62Medium
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.63Easy
What does #undef directive do?
Answer: A
#undef cancels the definition of a macro, allowing it to be redefined later. After #undef, the macro name becomes undefined.
Q.64Medium
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.65Medium
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.
Advertisement
Q.66Medium
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.67Hard
What will happen if we define a macro with the same name as a C standard library function?
Answer: B
Macros are textual replacements that happen before compilation. If a macro has the same name as a library function, the macro replaces all occurrences of that name.
Q.68Hard
Consider the macro: #define AREA(r) 3.14*r*r
What is the issue and how to fix it?
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.70Hard
Which of the following will correctly print the number of arguments passed to a variadic macro in C99?
Answer: A
C99 variadic macros don't have a built-in way to count arguments. Option A uses a clever technique with array size calculation. Option B references non-standard VA_NARGS. Options C and D use invalid syntax with token pasting.
Q.71Medium
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.72Medium
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.73Easy
In C99, which of the following is a valid use of token pasting operator (##)?
Answer: D
Both options A and B are valid. The ## operator pastes two tokens together. Whitespace around ## doesn't matter. #define CONCAT(a,b) a##b could be used as CONCAT(var, 1) to create var1.
Q.74Medium
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.75Easy
Which header file is required to use assert() macro in C?
Answer: A
assert.h contains the assert() macro which is useful for debugging. It tests a condition and aborts the program if the condition is false. This macro is only active when NDEBUG is not defined.
Q.76Hard
Consider: #define SWAP(a,b) {int temp=a; a=b; b=temp;}
If used in an if-else without braces, which problem occurs?
if(condition) SWAP(x,y); else printf("No swap");
Answer: C
The macro expands to {int temp=a; a=b; b=temp;}; which ends with a semicolon. This creates: if(condition) {...}; else... The semicolon after the block makes else unreachable. The macro should use do-while(0) wrapper or be wrapped in a function.
Q.77Hard
What does the following preprocessor output?
#define VERSION "2024"
#define STR(x) #x
printf(STR(VERSION));
Answer: D
Stringification happens before macro expansion of arguments. STR(VERSION) becomes "VERSION" (a string literal of the token VERSION), not the value of VERSION. To stringify the expanded value, use indirect stringification with two levels of macros.
Q.78Medium
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.79Hard
What will be the output of:
#define MAX(a,b) ((a)>(b)?(a):(b))
int main() { printf("%d", MAX(5++, 10)); return 0; }
Answer: C
The macro expands to ((5++)>(10)?(5++):(10)). The variable 5++ is evaluated multiple times due to macro substitution, causing undefined behavior. Increment operations in macro arguments should be avoided. This highlights why inline functions or constexpr functions are preferred in modern C++.
Q.80Medium
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.