enumerations

Discussion in 'C Programming' started by InuY4sha, Aug 2, 2009.

  1. InuY4sha

    InuY4sha Guest

    if I have 2 files (say foo1.c and foo2.c) which link within the same
    application, can I use 2 enumerations with the same names?
    Example:

    //foo1.c
    typedef enum{
    HELLO_WORLD,
    SPECIFIC_NUM1
    }foo1_enum;

    //foo2.c
    typedef enum{
    SPECIFIC_NUM2,
    HELLO_WORLD
    }foo2_enum;

    Does it complain for the HELLO_WORLD enumeration?
     
    InuY4sha, Aug 2, 2009
    #1
    1. Advertising

  2. InuY4sha

    Eric Sosman Guest

    InuY4sha wrote:
    > if I have 2 files (say foo1.c and foo2.c) which link within the same
    > application, can I use 2 enumerations with the same names?
    > Example:
    >
    > //foo1.c
    > typedef enum{
    > HELLO_WORLD,
    > SPECIFIC_NUM1
    > }foo1_enum;
    >
    > //foo2.c
    > typedef enum{
    > SPECIFIC_NUM2,
    > HELLO_WORLD
    > }foo2_enum;
    >
    > Does it complain for the HELLO_WORLD enumeration?


    Not if they're in separately-compiled files, no. The
    identifiers declared here have no linkage, so they are not
    "visible" outside their respective scopes.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Aug 2, 2009
    #2
    1. Advertising

  3. InuY4sha

    InuY4sha Guest

    On Aug 2, 9:25 pm, "Malcolm McLean" <> wrote:
    > "InuY4sha" <> wrote in message news:
    > > if I have 2 files (say foo1.c and foo2.c) which link within the same
    > > application, can I use 2 enumerations with the same names?
    > > Example:

    >
    > > //foo1.c
    > > typedef enum{
    > >   HELLO_WORLD,
    > >   SPECIFIC_NUM1
    > > }foo1_enum;

    >
    > > //foo2.c
    > > typedef enum{
    > >   SPECIFIC_NUM2,
    > >   HELLO_WORLD
    > > }foo2_enum;

    >
    > > Does it complain for the HELLO_WORLD enumeration?

    >
    > If you don't export the symbols they have file scope, so the fact that you
    > link won't cause a complaint. However usually you would want an enum in a
    > header. There you would get a clash if you included both. Unfortunately C
    > isn't clever enough to match up the enum symbol to the type of variable
    > holding it.


    Thanks (to Eric too),
    the reason for my question is that I need a way to describe exceptions
    by means of an enumeration. BUT, when I change file I can have some
    exception that are generic and hence preserved, while others do not
    fit for the new kind of data structure described by a file...

    Maybe I can explain even better if I tell you the whole thing in mind:
    I'd like a way to track exception and the SOURCE FILE / FUNCTION where
    those happened. What I do now is the following define for each file:
    (Given the filename is foo.c):

    #define foo_exception(foo_enum e) (exception("foo.c", (unsigned int)
    e))
     
    InuY4sha, Aug 2, 2009
    #3
  4. InuY4sha

    Nobody Guest

    On Sun, 02 Aug 2009 12:16:50 -0700, InuY4sha wrote:

    > if I have 2 files (say foo1.c and foo2.c) which link within the same
    > application, can I use 2 enumerations with the same names?
    > Example:
    >
    > //foo1.c
    > typedef enum{
    > HELLO_WORLD,
    > SPECIFIC_NUM1
    > }foo1_enum;
    >
    > //foo2.c
    > typedef enum{
    > SPECIFIC_NUM2,
    > HELLO_WORLD
    > }foo2_enum;
    >
    > Does it complain for the HELLO_WORLD enumeration?


    Enumerators are constants. They don't appear as symbols in the object
    file, so there won't be a linking conflict.

    If you were to put the types into header files, and include both headers
    from the same source file, then you would get an error during compilation.
     
    Nobody, Aug 2, 2009
    #4
  5. On Sun, 2 Aug 2009 20:25:32 +0100, "Malcolm McLean"
    <> wrote:

    >
    > "InuY4sha" <> wrote in message news:
    > > if I have 2 files (say foo1.c and foo2.c) which link within the same
    > > application, can I use 2 enumerations with the same names?
    > > Example:
    > >
    > > //foo1.c
    > > typedef enum{
    > > HELLO_WORLD,
    > > SPECIFIC_NUM1
    > > }foo1_enum;
    > >
    > > //foo2.c
    > > typedef enum{
    > > SPECIFIC_NUM2,
    > > HELLO_WORLD
    > > }foo2_enum;
    > >
    > > Does it complain for the HELLO_WORLD enumeration?
    > >

    > If you don't export the symbols they have file scope, so the fact that you
    > link won't cause a complaint.


    If by 'export' you mean 'give external linkage', you can't do that at
    all for enum-values, or enum struct or union types or typedefs. They
    don't even exist at link time in the usual separate-compilation model.
    Only (some) objects (variables), and functions, have either internal
    OR external linkage.

    > However usually you would want an enum in a
    > header. There you would get a clash if you included both.


    If you want enums/types, or macros, shared across multiple translation
    units, you have to put them in a (shared) header, or otherwise
    duplicate the text (e.g. in your source-control system).

    I'm not sure about 'usually' though. If the enum is part of the
    interface to other modules, yes it belongs in .h. If it is only used
    in the implementation, it's perfectly sensible in .c. IME (not
    statistically valid) both occur, I would estimate about equally.

    > Unfortunately C
    > isn't clever enough to match up the enum symbol to the type of variable
    > holding it.
    >

    For enum-values, yes. For enum-types, sort of; in C (unlike C++) they
    are only aliases for builtin integer types, but different enum types
    MAY be different integer types, so the object type is affected by the
    enum type. But many (most?) compilers don't bother and just use int.

    And since most (symbolic) debuggers treat all symbols as global, or at
    least all file-scope ones, even having the different enums in
    different t.u.s, which is fine for compile and link, will cause chaos
    in debugging -- if you use a debugger, which is a FArguedQ <G?>.
     
    David Thompson, Aug 17, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. clintonG

    HttpStatusCode Enumerations...

    clintonG, May 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,990
    clintonG
    May 6, 2004
  2. exquisitus
    Replies:
    12
    Views:
    22,554
    Tilman Bohn
    Feb 19, 2005
  3. exquisitus
    Replies:
    2
    Views:
    459
    Tor Iver Wilhelmsen
    Apr 24, 2005
  4. Chanchal
    Replies:
    4
    Views:
    407
    Thomas Hawtin
    Nov 29, 2005
  5. Joyce
    Replies:
    1
    Views:
    2,136
    Stan Kitsis [MSFT]
    Feb 18, 2005
Loading...

Share This Page