C Programming - C Preprocessor - Discussion


Will the program compile successfully?

#define X (4+Y)
#define Y (X+3)

int main()
    printf("%d\n", 4*X+2);
    return 0;

[A]. Yes
[B]. No

Answer: Option B


Reports an error: Undefined symbol 'X'

Kamaldua said: (Oct 1, 2011)  
X (4+Y) is a macro with arguments which is not been defined. X inside main is a variable not declared. Both Xs are different.

Sudarshan said: (Aug 17, 2012)  
How do you define macro inside main ?

Teju said: (Sep 30, 2012)  
Macro once defined outside the program is applicable through the program how come it fails compilation? X is declared right ?can anyone please give explanation for this?

Ankur Sharma said: (Nov 3, 2012)  
#define X (4+Y)
#define Y 3

int main()
printf("%d\n", 4 * X + 2);
return 0;

This will compile and will give 30 as output, in question, there will be compilation problem as both macros are recursive.

Abhinav said: (May 24, 2013)  
@Ankur is right.

The Error is because of recursion among macros X and Y not because of declaration because macros have global declaration, they need not to be defined inside main().

Harikrishna Reddy said: (Oct 2, 2013)  
See. once in printf statement,


=>4*(4+Y)+2 // because X=4+Y.

=>4*(4+X+3)// because Y=X+3.

So the compiler gives error as X is Undefined symbol.

Kousalya said: (Apr 1, 2019)  
In printf the value of 4*x+2.

4*(4+y)+2 this process will continue until stack gets filled. I think it's not compilation error.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.