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");
ANo problem, it will work correctly
BSyntax error during compilation
CThe else statement becomes unreachable
DThe macro cannot be used in conditional statements
Correct Answer:
C. The else statement becomes unreachable
EXPLANATION
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.
Which header file is required to use assert() macro in C?
Aassert.h
Bstdlib.h
Cstdio.h
Derror.h
Correct Answer:
A. assert.h
EXPLANATION
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.
What is the purpose of #line directive in C preprocessing?
ATo include a specific line from another file
BTo specify the filename and line number for error reporting
CTo create a line continuation in macro definition
DTo mark the end of a preprocessor block
Correct Answer:
B. To specify the filename and line number for error reporting
EXPLANATION
#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"
In C99, which of the following is a valid use of token pasting operator (##)?
A#define CONCAT(a,b) a##b
B#define CONCAT(a,b) a ## b
C#define CONCAT(a,b) a## ##b
DBoth A and B
Correct Answer:
D. Both A and B
EXPLANATION
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.
What will be the result of executing the following?
#define PRINT(x) printf(#x)
int main() { int a = 10; PRINT(a); return 0; }
Aa
B10
CCompilation error
Da10
Correct Answer:
A. a
EXPLANATION
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.
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; }
A25
B36
C11
D30
Correct Answer:
C. 11
EXPLANATION
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.
Correct Answer:
A. #define COUNT_ARGS(...) sizeof((int[]){__VA_ARGS__})/sizeof(int)
EXPLANATION
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.
Consider the preprocessor directive #define MAX 100. If this macro is used in multiple source files, which statement is TRUE?
AThe macro definition will be duplicated in each object file, increasing binary size
BThe macro is globally shared across all files without duplication
CIt will cause a linker error due to multiple definitions
DThe macro will only work in the file where it is defined
Correct Answer:
A. The macro definition will be duplicated in each object file, increasing binary size
EXPLANATION
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.
What is the difference between #include and #include "stdio.h"?
ANo difference, both are same
B searches standard directories; "" searches current directory first
C"" is for user-defined headers; is for system headers
DBoth B and C
Correct Answer:
D. Both B and C
EXPLANATION
Angle brackets <> search system/standard directories. Quotes "" search current directory first, then standard directories. Used for custom headers vs system headers respectively.