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.462Medium
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.463Medium
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.464Medium
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.465Medium
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.
Advertisement
Q.466Medium
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.467Medium
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.468Medium
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.469Medium
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.470Medium
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.471Medium
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.472Medium
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.473Medium
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.474Medium
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.475Medium
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.476Medium
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.477Medium
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.478Medium
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.479Medium
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.480Medium
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.