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 a 2
main()
{
    int r;
    #define a 5
    r=a*2;
    printf("%d",r);
}

 

  • A.10
  • B.4
  • C.2
  • D.5

Answer: A

Correct answer: 10

The preprocessor works on the text in order and ignores the braces entirely, so by the time it reaches r = a*2; the macro a has been redefined to 5. The statement becomes r = 5*2;, giving 10. Expect a redefinition warning, since there is no #undef in between.

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

Post your comment

Please log in or register to post a comment.