Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

#include <stdio.h>
#define str(x) #x
#define Xstr(x) str(x)
#define oper multiply

int main()
{
    char *opername = Xstr(oper);
    printf("%s\n", opername);
    return 0;
}

 

  • A.Error: in macro substitution
  • B.Error: invalid reference 'x' in macro
  • C.print 'multiply'
  • D.No output

Answer: C

Correct answer: print 'multiply'

This is the standard two-level stringizing idiom. A macro argument is not expanded before # is applied, so str(oper) would give "oper". Going through Xstr first forces the argument to be expanded - oper becomes multiply - and only then is it stringized, giving "multiply".

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.