ProgrammingVery hardProduct company OA
What is printed?
What is printed?
#define STR(x) #x
#define XSTR(x) STR(x)
#define VERSION 7
int main(void) {
printf("%s %s", STR(VERSION), XSTR(VERSION));
}- A. 7 7
- B. VERSION VERSION
- C. 7 VERSION
- D. VERSION 7
Correct answer: D. VERSION 7
STR(VERSION) stringizes its argument literally before VERSION gets a chance to expand, giving "VERSION". XSTR adds one extra level of macro expansion: VERSION is expanded to 7 as an argument to STR first (via XSTR's own expansion), and then STR stringizes the already-expanded 7, giving "7". This two-layer trick is the standard way to stringize an expanded macro value.
More C Programming questions
- What is printed? c #define SWAP(a, b) do { int t = a; a = b; b = t; } while (0) int main(void) { int x = 1, y = 2; if (x < y) SWAP(x, y)…
- What is printed? c #define A 1 #define B (A + 1) #undef A #define A 10 int main(void) { printf("%d", B); }
- What is printed? c #define FOO 10 #define BAR FOO + 5 int main(void) { int x = BAR 2; printf("%d", x); }
- What is printed? c #define LOG(msg) printf("[LOG] %s line %dn", msg, LINE) int main(void) { LOG("start"); LOG("end"); }
- What is printed? c #define TRUE 1 #define FALSE 0 #define BOOL int BOOL flag = TRUE; int main(void) { if (flag == TRUE) printf("yes")…
- What is printed? c #define CLAMP(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x)) int main(void) { printf("%d %d %d", CLAMP(5, 1…