Thread: C datatype, enumerator

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    25

    C datatype, enumerator

    Hi

    Is there any method to restrict the enumerated variable in C to occupy only one byte? I am using gcc (GCC) versin 4.0.0...

    please comment

    thanks

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I'm pretty sure an enum is supposed to take only as much space as it needs. There is a compiler switch in gcc that tells it to always use int as storage size but that should be off by default.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In general, no there isn't.
    In C, enums are just named integers, so that is what their size is. C++ makes a better job of it on the size front.
    http://david.tribble.com/text/cdiffs.htm#C99-enum-const

    Did you think of doing this?
    Quote Originally Posted by gcc manual
    -fshort-enums
    Allocate to an enum type only as many bytes as it needs for the declared range of possible values. Specifically, the enum type will be equivalent to the smallest integer type which has enough room.

    Warning: the -fshort-enums switch causes GCC to generate code that is not binary compatible with code generated without that switch. Use it to conform to a non-default application binary interface.
    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.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Would it not be legal to cast it into 1 byte?
    Code:
    #include <stdio.h>
    
    enum foo_t {
        FOO_TEST=0,
        FOO_BAR
    };
    
    int main(void)
    {
        char abc = (char)FOO_TEST;
        return 0;
    }
    Or have I completely missed the point?
    Last edited by zacs7; 07-26-2007 at 12:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing back objects from with functions. Datatype?
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2009, 11:50 AM
  2. Datatype
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 08-04-2008, 12:53 PM
  3. Casting Enumerator?!?
    By mdoland in forum C# Programming
    Replies: 1
    Last Post: 12-10-2007, 05:43 AM
  4. How to Convert a byte datatype to char or string
    By kvp_vivek in forum C Programming
    Replies: 3
    Last Post: 06-17-2006, 02:20 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM