Thread: enumeration/endianess

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    210

    Question enumeration/endianess

    When exchanging data (in my case usually integers) between big/little endian machines, I need to take care of proper byte ordering.
    Now I need to exchange an enum and was wondering if the same rules apply and if I can assume enums to always have the same size as integers.

    I assume I have to reorder bytes but since I'm not sure I thought I'd better ask.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I think sizeof(an enum) == sizeof(int) all the time in C
    IIRC, the rules are a little more complex for C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Salem
    I think sizeof(an enum) == sizeof(int) all the time in C
    Thanks Salem!

    I did some testing with different compiler options which seems to confirm that. Hopefully that's also true for non-GCC.
    I'm still not 100% sure about the endianess issue, but since the enum is probably just a wrapped int, I'll just assume I need to correct byteordering.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you have then use the htonl() family of functions.
    http://www.rt.com/man/htonl.3.html
    http://www.rt.com/man/ntohl.3.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed