C Programming — Preprocessor
C language from basics to advanced placement prep
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in Preprocessor
Q.21 Hard Preprocessor
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");
A No problem, it will work correctly
B Syntax error during compilation
C The else statement becomes unreachable
D The 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.

Take Test
Q.22 Easy Preprocessor
Which header file is required to use assert() macro in C?
A assert.h
B stdlib.h
C stdio.h
D error.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.

Take Test
Q.23 Medium Preprocessor
What is the purpose of #line directive in C preprocessing?
A To include a specific line from another file
B To specify the filename and line number for error reporting
C To create a line continuation in macro definition
D To 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"

Take Test
Q.24 Easy Preprocessor
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
D Both 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.

Take Test
Q.25 Medium Preprocessor
What will be the result of executing the following?
#define PRINT(x) printf(#x)
int main() { int a = 10; PRINT(a); return 0; }
A a
B 10
C Compilation error
D a10
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.

Take Test
Q.26 Medium Preprocessor
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; }
A 25
B 36
C 11
D 30
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.

Take Test
Q.27 Hard Preprocessor
Which of the following will correctly print the number of arguments passed to a variadic macro in C99?
A #define COUNT_ARGS(...) sizeof((int[]){__VA_ARGS__})/sizeof(int)
B #define COUNT_ARGS(...) VA_NARGS(__VA_ARGS__)
C #define COUNT_ARGS(args...) args##__count
D #define COUNT_ARGS(...) __VA_ARGS__##_count
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.

Take Test
Q.28 Medium Preprocessor
Consider the preprocessor directive #define MAX 100. If this macro is used in multiple source files, which statement is TRUE?
A The macro definition will be duplicated in each object file, increasing binary size
B The macro is globally shared across all files without duplication
C It will cause a linker error due to multiple definitions
D The 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.

Take Test
Q.29 Medium Preprocessor
What is the difference between #include and #include "stdio.h"?
A No difference, both are same
B searches standard directories; "" searches current directory first
C "" is for user-defined headers; is for system headers
D Both 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.

Take Test
Q.30 Hard Preprocessor
Consider the macro: #define AREA(r) 3.14*r*r
What is the issue and how to fix it?
A No issue, it works correctly
B Issue: missing parentheses; Fix: #define AREA(r) (3.14*(r)*(r))
C Issue: type mismatch; Fix: use long double
D Issue: cannot use floats in macros
Correct Answer:  B. Issue: missing parentheses; Fix: #define AREA(r) (3.14*(r)*(r))
EXPLANATION

Without parentheses, AREA(2+1) becomes 3.14*2+1*2+1 = 11.28. Proper version: #define AREA(r) (3.14*((r))*((r))) protects argument evaluation.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips