Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

10 views
Skip to first unread message

Chad

unread,
Mar 31, 2010, 3:42:41 PM3/31/10
to
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

Eric Sosman

unread,
Mar 31, 2010, 3:49:15 PM3/31/10
to

The inner declaration in the contained scope "shadows" or
"hides" the outer declaration in the containing scope.

int x = 42;
// `x' refers to the file-scope int
void f(void) {
// `x' still refers to the file-scope int
double x = 42.0;
// `x' now refers to a local double
int i;
for (i = 0; i < 10; ++i) {
// `x' still refers to the local double
char x[100];
// `x' now refers to a local array of char
...
}
// `x' refers to the local double again
...
}
// `x' refers to the file-scope int again

--
Eric Sosman
eso...@ieee-dot-org.invalid

Alexander Bartolich

unread,
Mar 31, 2010, 3:49:49 PM3/31/10
to
Chad wrote:
> [...] 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.

--

bartc

unread,
Mar 31, 2010, 4:49:58 PM3/31/10
to

"Chad" <cda...@gmail.com> wrote in message
news:f12bba4f-44b7-4b54...@z11g2000yqz.googlegroups.com...

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

Try moving enum values{} inside main().

--
Bartc

christian.bau

unread,
Mar 31, 2010, 3:50:54 PM3/31/10
to
On Mar 31, 8:42 pm, Chad <cdal...@gmail.com> wrote:

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

Both D's are in different scopes. The int D is enclosed in { } and
therefore D will always mean the "int D" within those braces. You
could even write

int main (void)
{
int D = 44;
{
int D = 113;
printf ("%d\n", D); // prints 113
}
printf ("%d\n", D); // prints 44
}

Ben Bacarisse

unread,
Mar 31, 2010, 5:59:44 PM3/31/10
to
Chad <cda...@gmail.com> writes:
<snip>
> enum values {
<snip>

> 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.

--
Ben.

Dann Corbit

unread,
Mar 31, 2010, 6:02:45 PM3/31/10
to
In article
<0.e4c2cbedea2b6a7e77f1.2010...@bsb.me.uk>,
ben.u...@bsb.me.uk says...

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

---
output placed in _LINT.TMP

Keith Thompson

unread,
Mar 31, 2010, 6:40:19 PM3/31/10
to
Ben Bacarisse <ben.u...@bsb.me.uk> writes:
[...]

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

Typo: it's "-Wshadow".

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

0 new messages