what is the scope of enum?

  • Thread starter Rouben Rostamian
  • Start date
R

Rouben Rostamian

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?
 
B

Ben Pfaff

1. Is an enumeration type, as described above, amenable
to external linkage?

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

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'.
3. If no, where in the standard is this behavior specified?

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

Rouben Rostamian

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

Thanks for clarification and reference. This helps a lot.
 
C

CBFalconer

Rouben said:
.... snip ...

Suppose I have:

enum myenum { enuma, enumb, enumc };

near the top of a file, outside any functions.
.... snip ...

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?

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

Frank Schmied

[...]
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?

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\,----../ `-..=.>"" _\,_
_______;/____<_ \_______\ \___////______;[email protected]_______
,"/ `.) `.) ```
/," /7__ /7_
(( ' \\\ )))
)
/
 
D

Derrick Coetzee

Rouben said:
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.

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
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top