C datatype, enumerator

This is a discussion on C datatype, enumerator within the C Programming forums, part of the General Programming Boards category; Hi Is there any method to restrict the enumerated variable in C to occupy only one byte? I am using ...

  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
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    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
    32,848
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 01: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, 12:50 PM
  2. Datatype
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 08-04-2008, 01: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, 03:20 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21