Hi,

I am sorry, I wrote incorrectly in the earlier mail. That

"I could see the difference in the size of enums when I toggle the option. If 
the option is not given, then all the enum occupy the same 4 bytes irrespective 
of their value and 2147483647 is the maximum value that can fit in integer. But 
when I specify the option, the size varies and 2147483647 starts fitting in".


But 

I WANT TO KNOW, HOW ENUMS ARE HANDLED IN GCC. HOW DO WE MAP AN ENUM VALUE TO 
THE CORRESPONDING INTEGER SIZE.

WHAT DOES THE OPTION -FSHORT-ENUMS DOES. PLZ EXPLAIN ME IN DETAIL.

I WANT TO KNOW, GIVEN AN ENUM VALUE, HOW DO WE CALCULATE OR DETERMINE THAT WHAT 
SIZE SHOULD THE ENUM OCCUPY.

Does all the enum have a same size of 4 bytes or does it varies depending on 
the initializing value?


I am not able to understand the behavior of this program


#include <stdio.h>

int main()
{
        enum abc{
        a, b=125, c
        };

        enum pqr {
        p, q=65537, r
        };

        enum xyz{
        x, y=2147483646, z              //-----------(X)
        };

        enum lmn{
        l, m=4294967295, n              //-----------(Y)
        };

        printf("a=%d %d\n", a, sizeof(a));
        printf("c=%d %d\n", c, sizeof(c));
        printf("p=%d %d\n", p, sizeof(p));
        printf("r=%d %d\n", r, sizeof(r));
        printf("x=%d %d\n", x, sizeof(x));
        printf("z=%d %d\n", z, sizeof(z));
        printf("l=%d %d\n", l, sizeof(l));
        printf("n=%d %d\n", n, sizeof(n));


        return 0;
}

Plz note that in the line marked (X) if I mension 2147483647 it gives
overflow error. 

However it is able to compile with the higher value given in the next enum
(line marked).

The output of this tc is even when fsort-enums option is given.

a=0 4
c=126 4
p=0 4
r=65538 4
x=0 4
z=2147483647 4
l=0 4
n=0 8

However if I mension 2147483648 program compiles again. With output

a=0 4
c=126 4
p=0 4
r=65538 4
x=0 4
z=-2147483647 4
l=0 4
n=0 8

Regards
Gaurav

-----Original Message-----
From: Gaurav Gautam, Noida 
Sent: Saturday, August 27, 2005 1:56 PM
To: 'gcc@gcc.gnu.org'
Subject: help: about enum

Hi,

Plz help me
I want to know, how enums are handled in gcc. How do we map an enum value to 
the corresponding integer size.

What does the option -fshort-enums does. Plz explain me in detail.

I could see the difference in the size of enums when I toggle the option. If 
the option is not given, then all the enum occupy the same 4 bytes irrespective 
of their value and 2147483647 is the maximum value that can fit in integer. But 
when I specify the option, the size varies and 2147483647 starts fitting in.
Plz explain this behaviour and how does it confirms to standard.

I want to know, given an enum value, how do we calculate or determine that what 
size should the enum occupy.

Regards
Gaurav

Reply via email to