C Preprocessor Questions

Practice C Preprocessor MCQs with answers and explanations. Page 1 of 2.

Category
C Programming
Topic
C Preprocessor
Page
1 / 2
Mode
Practice

Questions

Open any question to view the answer and explanation.

Macro pitfalls: what does this program print? #include<stdio.h> #define SQR(x) (xx) int main() { int a, b = 3; a = SQR(b+2); // expands to (b+2b+2) printf("%d ", a); return 0; } Assume usual C operator precedence.
Open
View answer
String literals and macros: what does this program print? #include<stdio.h> #define MESS junk int main() { printf("MESS "); return 0; } Does the macro affect the content of the string literal?
Open
View answer
Operator precedence and unsafe macros: predict the result. #include<stdio.h> #define SQUARE(x) xx int main() { float s = 10, u = 30, t = 2, a; a = 2(s - ut) / SQUARE(t); // expands to 2(s - ut) / t * t printf("Result = %f", a); return 0; } What will be printed?
Open
View answer
Macros with side effects: what does this program output, and why is it dangerous? #include<stdio.h> #define CUBE(x) (xxx) int main() { int a, b = 3; a = CUBE(b++); // expands to (b++b++b++) → undefined behavior printf("%d, %d ", a, b); return 0; } Choose the most appropriate output often seen on popular compilers, noting the caveat in the explanation.
Open
View answer
In C preprocessor macros, what will be the exact output of this program (note the macro parameter name and the printed literal)? #include<stdio.h> #define PRINT(int) printf("int=%d, ", int); int main() { int x = 2, y = 3, z = 4; PRINT(x); PRINT(y); PRINT(z); return 0; }
Open
View answer
C macros with stringification: what does this program print? #include<stdio.h> #define JOIN(s1, s2) printf("%s=%s %s=%s ", #s1, s1, #s2, s2) int main() { char *str1 = "India"; char *str2 = "CURIOUSTAB"; JOIN(str1, str2); return 0; }
Open
View answer
Macro with do–while and external counter: how many times does it print? #include<stdio.h> #define FUN(arg) do { if (arg) printf("CuriousTab..."); } while (--i) int main() { int i = 2; FUN(i < 3); return 0; }
Open
View answer
Two-level stringification in C macros: what does this program print? #include<stdio.h> #define str(x) #x #define Xstr(x) str(x) #define oper multiply int main() { char *opername = Xstr(oper); printf("%s ", opername); return 0; }
Open
View answer
Macro argument expressions and operator precedence: what will this program print? #include<stdio.h> #define MAX(a, b) (a > b ? a : b) int main() { int x; x = MAX(3+2, 2+7); printf("%d ", x); return 0; }
Open
View answer
Evaluate a simple SWAP macro with the comma operator: what is the output? #include<stdio.h> #define SWAP(a, b) int t; t = a, a = b, b = t; int main() { int a = 10, b = 12; SWAP(a, b); printf("a = %d, b = %d ", a, b); return 0; }
Open
View answer
Follow macro expansion and integer precedence: what does this program print? #include<stdio.h> #define MIN(x, y) (x < y) ? x : y int main() { int x = 3, y = 4, z; z = MIN(x + y/2, y - 1); if (z > 0) printf("%d ", z); return 0; }
Open
View answer
Macro that prints integers without spaces: what will this program output exactly? #include<stdio.h> #define PRINT(i) printf("%d,", i) int main() { int x = 2, y = 3, z = 4; PRINT(x); PRINT(y); PRINT(z); return 0; }
Open
View answer
Token pasting (##) in macros: what does this program print? #include<stdio.h> #define FUN(i, j) i##j int main() { int va1 = 10; int va12 = 20; printf("%d ", FUN(va1, 2)); return 0; }
Open
View answer
Side effects in macro arguments: evaluate this program's output and explain the increments. #include<stdio.h> #define MAN(x, y) ((x) > (y)) ? (x) : (y) int main() { int i = 10, j = 5, k = 0; k = MAN(++i, j++); printf("%d, %d, %d ", i, j, k); return 0; }
Open
View answer
In C, the macro with three operands expands without extra parentheses. Determine the final value printed by this program. #include<stdio.h> #define MAX(a, b, c) (a>b? a>c? a : c: b>c? b : c) int main() { int x; x = MAX(3+2, 2+7, 3+7); printf("%d ", x); return 0; }
Open
View answer
C preprocessor capabilities: can it detect missing declarations, nested comments, or mismatched braces at compile time?
Open
View answer
Definition check: Is a preprocessor directive best described as a message from the programmer to the preprocessor?
Open
View answer
Purpose recognition: Is the sequence #ifdef … #else … #endif used for conditional compilation in C/C++?
Open
View answer
Compare macro invocation and function call semantics: Do macro calls and function calls behave exactly the same?
Open
View answer
C/C++ preprocessor behavior: What happens if an #include header cannot be found in the search paths?
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.