__attribute__ ((mode(__byte__)))

Discussion in 'C Programming' started by JeromeArbezGindre@gmail.com, Oct 9, 2007.

  1. Guest

    Hi,

    I would like to map an enum on a byte,so here is my code:


    enum my_enum {
    A = 1,
    B = 2,
    } __attribute__ ((mode(__byte__))) ;

    int is_A (enum my_enum my_val)
    {
    return (my_val == A) ;
    }

    When compiled, I have the following:

    cc -c -o toto.o toto.c
    toto.c: In function 'is_A':
    toto.c:10: warning: comparison is always false due to limited range of
    data type

    I do not have any problem when i transfrom is_A :
    int is_A (unsigned char my_val)
    {
    return (my_val == A) ;
    }

    So, muy question is :

    Is there a correct way to declare an enum fo map it on a byte.

    Thanks

    Jérôme
    , Oct 9, 2007
    #1
    1. Advertising

  2. Mark Bluemel Guest

    wrote:
    > Hi,
    >
    > I would like to map an enum on a byte,so here is my code:


    I don't know quite what you mean by this, or why you feel it's necessary

    > enum my_enum {
    > A = 1,
    > B = 2,
    > } __attribute__ ((mode(__byte__))) ;



    This, from the rapid websearch I've done, appears to be specific to GCC.
    You'd do better posting to a GCC forum like gnu.gcc.help, I suspect.
    Mark Bluemel, Oct 9, 2007
    #2
    1. Advertising

  3. Eric Sosman Guest

    wrote:
    > Hi,
    >
    > I would like to map an enum on a byte,so here is my code:
    >
    >
    > enum my_enum {
    > A = 1,
    > B = 2,
    > }


    This much is C, ...

    > __attribute__ ((mode(__byte__))) ;


    .... but this is some compiler-specific (gcc?) extension to C.
    You'll need to check the compiler's documentation or find a
    forum where that compiler is discussed to learn exactly what
    this beyond-C stuff does and how to use it wisely.

    > [...]
    > Is there a correct way to declare an enum fo map it on a byte.


    The only thing C guarantees is that the type "behind" the
    enum will be some kind of an integer, whose range of values
    includes all the named enum constants (A and B, in your code).
    The compiler is free to choose the type, and is not required
    to explain its choices. You can influence the compiler's choice
    only in an indirect and limited way: For example, using a named
    constant with a negative value prevents the compiler from choosing
    an unsigned type. Other than that, though, there is no portable
    way to control what the compiler uses as an underlying type.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 9, 2007
    #3
  4. Guest

    On Oct 9, 3:04 pm, Mark Bluemel <> wrote:
    > wrote:
    > > I would like to map an enum on a byte,so here is my code:

    >
    > I don't know quite what you mean by this, or why you feel it's necessary


    This enum have to be mapped on a hardware register.

    > This, from the rapid websearch I've done, appears to be specific to GCC.
    > You'd do better posting to a GCC forum like gnu.gcc.help, I suspect.


    Sorry for the noise.

    Thanks.
    , Oct 9, 2007
    #4
  5. "" <> writes:
    > I would like to map an enum on a byte,so here is my code:
    >
    >
    > enum my_enum {
    > A = 1,
    > B = 2,
    > } __attribute__ ((mode(__byte__))) ;
    >
    > int is_A (enum my_enum my_val)
    > {
    > return (my_val == A) ;
    > }
    >
    > When compiled, I have the following:
    >
    > cc -c -o toto.o toto.c
    > toto.c: In function 'is_A':
    > toto.c:10: warning: comparison is always false due to limited range of
    > data type


    The code depends on a gcc extension. Consider whether you're using
    that extension properly. I won't comment further here; try the gcc
    documentation and/or gnu.gcc.help. (If you post to gnu.gcc.help, I
    might respond in more detail there.)

    [...]

    > Is there a correct way to declare an enum fo map it on a byte.


    There's no portable way to do so.

    But enums aren't really first-class types in C anyway. (We could
    argue for days about what "first-class type" means. Let's not.)
    There's a slightly ugly workaround that's really no uglier than using
    enums directly.

    If you want a type that's exactly one byte, use ``unsigned char''.
    It's guaranteed to be exactly one byte with no padding bits (though a
    byte isn't guaranteed to be only 8 bits).

    A declaration
    enum my_enum { A = 1, B = 2 };
    declares a type "enum my_enum", but the constants A and B are not of
    that type; they're actually of type int. This doesn't (usually) cause
    any problems because integer and enumerated types can be freely
    assigned to each other, with implicit conversion. <OT>I think C++
    does this differently.</OT>

    An "enum" declaration can be used to declare a series of constants
    without declaring a named type. This is a fairly common trick used to
    create named constants without using the preprocessor. The values are
    limited to the range of type int, but that's not a problem in your
    case.

    Putting all this together, you can declare the type and the constants
    to be used with that type separately:

    typedef unsigned char byte;
    enum { A = 1, B = 2 };

    int is_A(byte my_val)
    {
    return my_val == A;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 9, 2007
    #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. Krzysztof Kolago

    Problem with __attribute__

    Krzysztof Kolago, Nov 19, 2003, in forum: C++
    Replies:
    1
    Views:
    388
    Victor Bazarov
    Nov 19, 2003
  2. ritesh

    Error using '__attribute__'

    ritesh, May 18, 2006, in forum: C Programming
    Replies:
    5
    Views:
    385
    Jordan Abel
    May 18, 2006
  3. Replies:
    1
    Views:
    319
    Ian Collins
    Feb 28, 2007
  4. nagaraj
    Replies:
    0
    Views:
    497
    nagaraj
    Apr 14, 2008
  5. parag
    Replies:
    0
    Views:
    203
    parag
    Jun 13, 2011
Loading...

Share This Page