Gautam Kumar
Gautam Kumar

Reputation: 1170

symbolic constants

#include<conio.h>
#include<stdio.h>
#define abc 7

int main()
{

int abc=1;

printf("%d",abc);

getch();

return 0;
}

why this program is giving compile time error

Upvotes: 0

Views: 129

Answers (5)

muruga
muruga

Reputation: 2122

You declare "abc" macro value as 7 . So if again include the macro name as variable, it will give error.

consider the following

abc value is 7. So it will treated as 7=1. So that it will give error.

Upvotes: 2

Daniel Daranas
Daniel Daranas

Reputation: 22644

You define abc as 7.

Then int abc=1 is transformed into int 7=1 which is absurd.

Why are you doing this?

Upvotes: 2

Khaled Alshaya
Khaled Alshaya

Reputation: 96939

When the preprocessor replaces abc with 7, the following line becomes invalid:

int 7=1;

An identifier in C, can't be just a number.

Upvotes: 0

codaddict
codaddict

Reputation: 455460

The C preprocessor does a blind replacement of abc with 7 resulting in:

int 7=1;

which clearly is an error.

Upvotes: 0

Eli Bendersky
Eli Bendersky

Reputation: 273854

You're assigning 7=1 which is invalid. Since you've defined abc to be 7, the preprocessor translates the line:

int abc=1;

to:

int 7=1;

Which is a syntax error in C (my gcc says syntax error before numeric constant).

Upvotes: 3

Related Questions