How comes the compiler doesn't complain about conflicting variablesfor the following piece of code..

C

Chad

Given the following...

#include <stdio.h>

enum values {
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000
};

int main(void)
{
int D = 44;
enum values romandigits = D;
printf("The value is: %d\n", romandigits);
return 0;
}

I get....
[cdalten@localhost oakland]$ gcc -g -Wall -Wextra roman.c -o roman
[cdalten@localhost oakland]$ ./roman
The value is: 44
[cdalten@localhost oakland]$

How come the compiler doesn't complain about 'D' being both an int and
an enum?


Chad
 
A

Alexander Bartolich

Chad said:
[...] How come the compiler doesn't complain about 'D' being both
an int and an enum?

Because the declarations are in different scopes.
You do get an error if they are in the same scope.
And no, it's a feature.

--
 
B

bartc

Chad said:
Given the following...

#include <stdio.h>

enum values {
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000
};

int main(void)
{
int D = 44;
enum values romandigits = D;
printf("The value is: %d\n", romandigits);
return 0;
}

I get....
[cdalten@localhost oakland]$ gcc -g -Wall -Wextra roman.c -o roman
[cdalten@localhost oakland]$ ./roman
The value is: 44
[cdalten@localhost oakland]$

How come the compiler doesn't complain about 'D' being both an int and
an enum?

int D is in a local scope, it hides the outer D.

Try moving enum values{} inside main().
 
B

Ben Bacarisse

Chad said:
enum values {
D = 500,
M = 1000
};

int main(void)
{
int D = 44;
enum values romandigits = D;
printf("The value is: %d\n", romandigits);
return 0;
}

I get....
[cdalten@localhost oakland]$ gcc -g -Wall -Wextra roman.c -o roman
[cdalten@localhost oakland]$ ./roman
The value is: 44

How come the compiler doesn't complain about 'D' being both an int and
an enum?

You might choose to add -Wshaddow to your set of useful gcc options.
 
D

Dann Corbit

Chad said:
enum values {
D = 500,
M = 1000
};

int main(void)
{
int D = 44;
enum values romandigits = D;
printf("The value is: %d\n", romandigits);
return 0;
}

I get....
[cdalten@localhost oakland]$ gcc -g -Wall -Wextra roman.c -o roman
[cdalten@localhost oakland]$ ./roman
The value is: 44

How come the compiler doesn't complain about 'D' being both an int and
an enum?

You might choose to add -Wshaddow to your set of useful gcc options.

Lint is also handy:
C:\tmp>"C:\Lint\Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP) l.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software 1985-2006

--- Module: l.c (C)

C:\tmp>type _LINT.TMP | more

--- Module: l.c (C)
_
int D = 44;
l.c(15) : Warning 578: Declaration of symbol 'D' hides symbol
'values::D' (line
9)
l.c(9) : Info 830: Location cited in prior message
_
enum values romandigits = D;
l.c(16) : Error 64: Type mismatch (initialization) (int/enum)
_
}
l.c(19) : Note 953: Variable 'D' (line 15) could be declared as const
--- Eff.
C++ 3rd Ed. item 3
l.c(15) : Info 830: Location cited in prior message
_
}
l.c(19) : Note 953: Variable 'romandigits' (line 16) could be declared
as const
--- Eff. C++ 3rd Ed. item 3
l.c(16) : Info 830: Location cited in prior message

--- Wrap-up for Module: l.c

Info 749: local enumeration constant 'values::I' (line 4, file l.c) not
referenced
l.c(4) : Info 830: Location cited in prior message
Info 749: local enumeration constant 'values::V' (line 5, file l.c) not
referenced
l.c(5) : Info 830: Location cited in prior message
Info 749: local enumeration constant 'values::X' (line 6, file l.c) not
referenced
l.c(6) : Info 830: Location cited in prior message
Info 749: local enumeration constant 'values::L' (line 7, file l.c) not
referenced
l.c(7) : Info 830: Location cited in prior message
Info 749: local enumeration constant 'values::C' (line 8, file l.c) not
referenced
l.c(8) : Info 830: Location cited in prior message
Info 749: local enumeration constant 'values::D' (line 9, file l.c) not
referenced
l.c(9) : Info 830: Location cited in prior message
Info 749: local enumeration constant 'values::M' (line 10, file l.c) not
referenced
l.c(10) : Info 830: Location cited in prior message
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top