Connecting Tech Pros Worldwide Help | Site Map

what is the scope of enum?

Rouben Rostamian
Guest
 
Posts: n/a
#1: Nov 14 '05
I searched the C99 standard and clc's FAQ but was unable to
find an answer to the following issue. I hope that someone
here can illuminate me.

Suppose I have:

enum myenum { enuma, enumb, enumc };

near the top of a file, outside any functions.

Now, I don't want to have external linkage for this enum, that
is, I don't want it to be visible outside its translation unit.

So I added static to the declaration, as in:

static enum myenum { enuma, enumb, enumc };

When I compile this with gcc, it emits the message:

warning: useless keyword or type name in empty declaration

Therefore gcc does not like a static enum.

So I have the following questions:

1. Is an enumeration type, as described above, amenable
to external linkage?
2. If yes, then how does one hide a file-scope enum within
its translation unit?
3. If no, where in the standard is this behavior specified?

--
Rouben Rostamian
Ben Pfaff
Guest
 
Posts: n/a
#2: Nov 14 '05

re: what is the scope of enum?


rouben@pc18.math.umbc.edu (Rouben Rostamian) writes:
[color=blue]
> 1. Is an enumeration type, as described above, amenable
> to external linkage?[/color]

No. An enumeration type always has "no linkage".
[color=blue]
> 2. If yes, then how does one hide a file-scope enum within
> its translation unit?[/color]

If you're talking about an enumeration type, you need not take
any action, because an enumeration type has no linkage.

If you're talking about an object of enumeration type, you can
declare it `static'.
[color=blue]
> 3. If no, where in the standard is this behavior specified?[/color]

See C99 6.2.2#6:

6 The following identifiers have no linkage: an identifier
declared to be anything other than an object or a function;
an identifier declared to be a function parameter; a block
scope identifier for an object declared without the
storage-class specifier extern.

An enumeration type is an identifier other than an object or a
function (it is a type).
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Rouben Rostamian
Guest
 
Posts: n/a
#3: Nov 14 '05

re: what is the scope of enum?


In article <87fz1dkl0f.fsf@benpfaff.org>,
Ben Pfaff <blp@cs.stanford.edu> wrote:[color=blue]
>See C99 6.2.2#6:
>
>6 The following identifiers have no linkage: an identifier
> declared to be anything other than an object or a function;
> an identifier declared to be a function parameter; a block
> scope identifier for an object declared without the
> storage-class specifier extern.
>
>An enumeration type is an identifier other than an object or a
>function (it is a type).[/color]

Thanks for clarification and reference. This helps a lot.

--
Rouben Rostamian
CBFalconer
Guest
 
Posts: n/a
#4: Nov 14 '05

re: what is the scope of enum?


Rouben Rostamian wrote:[color=blue]
>[/color]
.... snip ...[color=blue]
>
> Suppose I have:
>
> enum myenum { enuma, enumb, enumc };
>
> near the top of a file, outside any functions.
>[/color]
.... snip ...[color=blue]
>
> 1. Is an enumeration type, as described above, amenable
> to external linkage?
> 2. If yes, then how does one hide a file-scope enum within
> its translation unit?
> 3. If no, where in the standard is this behavior specified?[/color]

I think the confusion arises because the "enum myenum ..."
statement is not declaring a variable, but a quasi-type, which is
identified by the complete string "enum myenum".

You could have followed this with:

enum myenum myvariable;

and myvariable could take on the enumerated values, be exported, or
marked static, etc.

I say quasi-type because C makes no attempts to restrict values
stored in myvariable to those enumerated.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Frank Schmied
Guest
 
Posts: n/a
#5: Nov 14 '05

re: what is the scope of enum?


On Fri, 7 Jan 2005 20:36:12 +0000 (UTC), rouben@pc18.math.umbc.edu
(Rouben Rostamian) wrote:

[...][color=blue]
>When I compile this with gcc, it emits the message:
>
> warning: useless keyword or type name in empty declaration
>
>Therefore gcc does not like a static enum.
>
>So I have the following questions:
>
>1. Is an enumeration type, as described above, amenable
> to external linkage?
>2. If yes, then how does one hide a file-scope enum within
> its translation unit?
>3. If no, where in the standard is this behavior specified?[/color]

Since an enum is a pure structural declaration, it has usually no
memory location, because it doesn't need any memory. It is primarily
used for code clarity and increment-definition - the compiler cares
about that, but usually emits no memory for this.
So any storage type modifiers are ignored: No memory location, no
storage class.
--
,,,
_ _ \(((.
__,,../v\,----../ `-..=.>"" _\,_
_______;/____<_ \_______\ \___////______;______pukys@pukys.de_______
,"/ `.) `.) ```
/," /7__ /7_
(( ' \\\ )))
)
/
Derrick Coetzee
Guest
 
Posts: n/a
#6: Nov 14 '05

re: what is the scope of enum?


Rouben Rostamian wrote:[color=blue]
> Now, I don't want to have external linkage for this enum, that
> is, I don't want it to be visible outside its translation unit.[/color]

One simple way of looking at it is that an enum is, in practice, much
like a sequence of #define statements:

enum myenum { enuma, enumb, enumc };

/* is about the same as */

#define enuma 0
#define enumb 1
#define enumc 2

You can see how the concept of linkage wouldn't apply, and how it does
in fact do what you want. Also see comp.lang.c FAQ question 2.22 at:

http://www.eskimo.com/~scs/C-faq/q2.22.html

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Closed Thread


Similar C / C++ bytes