ProgrammingVery hardProduct company OA

What is printed?

What is printed?
#define N 4
#define STR(x) #x
#define XSTR(x) STR(x)
#define CAT(a, b) a##b
#define XCAT(a, b) CAT(a, b)
int NN = 9;
printf("%s %s %d %d %zu", STR(N), XSTR(N), CAT(N, N), XCAT(N, N), sizeof(XSTR(N + 1)));
  1. A. N 4 9 44 6
  2. B. 4 4 44 44 6
  3. C. N 4 9 44 2
  4. D. N 4 44 44 6

Correct answer: A. N 4 9 44 6

A macro argument is fully expanded before substitution EXCEPT where it is an operand of # or ##. So STR(N) stringifies the spelling → "N", and CAT(N, N) pastes the raw tokens → NN, the variable holding 9. The extra level (XSTR, XCAT) lets N expand to 4 first: "4" and the pasted number 44. XSTR(N + 1) is the string "4 + 1" – no arithmetic happens in the preprocessor – whose size is 5 characters + '\0' = 6.

More C Programming questions

Exams that ask this topic