C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Yes / No Questions (Q.No. 1)
1.
Will the program compile successfully?
#include<stdio.h>
#define X (4+Y)
#define Y (X+3)

int main()
{
    printf("%d\n", 4*X+2);
    return 0;
}
Yes
No
Answer: Option
Explanation:
Reports an error: Undefined symbol 'X'
Discussion:
7 comments Page 1 of 1.

Kamaldua said:   1 decade ago
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:   1 decade ago
How do you define macro inside main ?

Teju said:   1 decade ago
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:   1 decade ago
#include<stdio.h>
#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:   1 decade ago
@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:   1 decade ago
See. once in printf statement,

=>4*X+2.

=>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:   5 years ago
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:

Your comments will be displayed after verification.