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