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)));- A. N 4 9 44 6
- B. 4 4 44 44 6
- C. N 4 9 44 2
- 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
- Which of the numbered statements violate a constraint (i.e. must be rejected by the compiler)? c typedef char strt; #define STRM char char…
- What is printed? c int g = 1; int f(int n) { static int s = 10; int g = n; s += g; { extern int g; g += s; } return s; } int main(void) {…
- What is printed? c int i = 0, j = 5, k = 0; int r = (i++ || j++) && (k++, i + j > 1) ? i++ : (j += 2, k); printf("%d %d %d %d", r, i, j, k);
- What is printed? c const char w[] = {"heap", "stack", "queue", "trie"}; const char p = w; printf("%c", ++p); printf("%c", ++p)…
- Assume 8-bit unsigned char. What is printed? c unsigned int count = 0; for (unsigned char i = 250; i != 4; i += 3) count++; printf("%u"…
- Assume 32-bit unsigned int and 64-bit unsigned long long. What is printed? c unsigned int a = 0x80000001u; unsigned int b = a 2; int c =…