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

Discussion

What will be the output of the following C code?

#include <stdio.h>
#define hello 10
main()
{
    #ifdef hello
    #undef hello
    #define hello 100
    #else
    #define hello 200
    #endif
    printf("%d",hello);
}

 

  • A.Error
  • B.10
  • C.100
  • D.200

Answer: C

Correct answer: 100

hello is defined, so the #ifdef branch runs: the old definition is removed with #undef and a new one, 100, put in its place. Using #undef before redefining is exactly the right way to do this, since it avoids the redefinition warning. The output is 100.

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

Post your comment

Please log in or register to post a comment.