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));
}
  1. A. 7 7
  2. B. VERSION VERSION
  3. C. 7 VERSION
  4. 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

Exams that ask this topic