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
RE: help: about enum
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 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
help: interfacing between C and fortran program
Hi, I have a function written in fortran say fun(x, y), with x and y as integer (scalars) . Function returns integer. I need to call this function from a C program. How do I do it. Can some one help me. Does Gfortran and gcc support this. ?? Regards Gaurav
No effect of -fshort-enums..is it a bug
Hi, I have compiled a testcase int main() { enum aa { a = 0, b =127 , c }; printf("size = %d %d %d\n", sizeof(a),sizeof(b), sizeof(c)); printf("value= %d %d %d\n", a,b,c); return 0; } On gcc (GCC) 4.1.0 20050915 (experimental) with the following option -fshort-enums. The option -fshort-enums has no effect and the output is same as it is without this option. I also tried the same tc on gcc (GCC) 3.3.1 (SuSE Linux). But in this case the output changed with and without this option. I am using an SuSe linux -- X86_AMD64 machine. I think it's a bug. Can anybody please confirm? Thanks Gaurav
RE: No effect of -fshort-enums..is it a bug
Thanks for the reply, But why is there a difference in the output of same tc, with an old gcc compiler and a new version of compiler. Was there a bug in the earlier gcc. I have a doubt. Gcc manual says that "-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." Does -fshort-enum guides the size of enumeration type or the size of enumerator constant ? After modifying the tc as #include int main() { enum aa { a = 0, b =127 , c }; printf("size = %d %d %d\n", sizeof(enum aa),sizeof(b), sizeof(c)); printf("value= %d %d %d\n", a,b,c); return 0; ) The output is size = 1 1 1 value= 0 127 128 when gcc (GCC) 3.3.1 (SuSE Linux) is used with -fshort-enums. And size = 1 4 4 value= 0 127 128 when (GCC) 4.1.0 20050915 (experimental) is used with -fshort-enums. Which of the two output is standard confirming.? > -Original Message- > From: Daniel Jacobowitz [mailto:[EMAIL PROTECTED] > Sent: Wednesday, September 21, 2005 6:10 PM > To: Gaurav Gautam, Noida > Cc: gcc@gcc.gnu.org; [EMAIL PROTECTED] > Subject: Re: No effect of -fshort-enums..is it a bug > > On Wed, Sep 21, 2005 at 05:46:58PM +0530, Gaurav Gautam, Noida wrote: > > int main() > > { > > enum aa { > > a = 0, b =127 , c > > }; > > > > printf("size = %d %d %d\n", sizeof(a),sizeof(b), sizeof(c)); > > printf("value= %d %d %d\n", a,b,c); > > return 0; > > } > > > The option -fshort-enums has no effect and the output is same as it is > without this option. > > It's not a bug. Add sizeof(enum aa) to your printf; _that_ will be > affected by -fshort-enums. The type of the enumerators remains int. > > -- > Daniel Jacobowitz > CodeSourcery, LLC
RE: No effect of -fshort-enums..is it a bug
Thanks for the reply, but I did not get the answer to my question. My question is: In the below mentioned program #include int main() { enum aa { a = 0, b =127 , c }; printf("size = %d %d %d\n", sizeof(enum aa),sizeof(b),sizeof(c)); printf("value= %d %d %d\n", a,b,c); return 0; ) The output is size = 1 1 1 value= 0 127 128 when gcc (GCC) 3.3.1 (SuSE Linux) is used with -fshort-enums. And size = 1 4 4 value= 0 127 128 when (GCC) 4.1.0 20050915 (experimental) is used with -fshort-enums. Please confirm which of the two outputs is correct and why is there a difference in the output of two versions of compiler? Thanks Gaurav gautam > -Original Message- > From: Gaurav Gautam, Noida > Sent: Wednesday, September 21, 2005 7:04 PM > To: 'gcc@gcc.gnu.org'; '[EMAIL PROTECTED]' > Cc: 'Daniel Jacobowitz' > Subject: RE: No effect of -fshort-enums..is it a bug > > Thanks for the reply, > > But why is there a difference in the output of same tc, with an old gcc > compiler and a new version of compiler. > > Was there a bug in the earlier gcc. > > I have a doubt. > > Gcc manual says that > > "-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." > > Does -fshort-enum guides the size of enumeration type or the size of > enumerator constant ? > > After modifying the tc as > > #include > int main() > { > enum aa { > a = 0, b =127 , c > }; > > printf("size = %d %d %d\n", sizeof(enum aa),sizeof(b), > sizeof(c)); > printf("value= %d %d %d\n", a,b,c); > return 0; > ) > > The output is > > size = 1 1 1 > value= 0 127 128 > when gcc (GCC) 3.3.1 (SuSE Linux) is used with -fshort-enums. > > > And > > size = 1 4 4 > value= 0 127 128 > > when (GCC) 4.1.0 20050915 (experimental) is used with -fshort-enums. > > Which of the two output is standard confirming.? > > > > -Original Message- > > From: Daniel Jacobowitz [mailto:[EMAIL PROTECTED] > > Sent: Wednesday, September 21, 2005 6:10 PM > > To: Gaurav Gautam, Noida > > Cc: gcc@gcc.gnu.org; [EMAIL PROTECTED] > > Subject: Re: No effect of -fshort-enums..is it a bug > > > > On Wed, Sep 21, 2005 at 05:46:58PM +0530, Gaurav Gautam, Noida wrote: > > > int main() > > > { > > > enum aa { > > > a = 0, b =127 , c > > > }; > > > > > > printf("size = %d %d %d\n", sizeof(a),sizeof(b), sizeof(c)); > > > printf("value= %d %d %d\n", a,b,c); > > > return 0; > > > } > > > > > The option -fshort-enums has no effect and the output is same as it is > > without this option. > > > > It's not a bug. Add sizeof(enum aa) to your printf; _that_ will be > > affected by -fshort-enums. The type of the enumerators remains int. > > > > -- > > Daniel Jacobowitz > > CodeSourcery, LLC
RE: No effect of -fshort-enums..is it a bug
> > > Hi Gaurav, > > > >> Please confirm which of the two outputs is correct and why is there a > > difference in the output of two versions of compiler? > > > > Both outputs are "correct". > > > > > No, the standard is entirely unambiguous: > > > 6.4.4.3 Enumeration constants > Syntax > 1 enumeration-constant: > identifier > Semantics > 2 An identifier declared as an enumeration constant has type int. > > > The enumeration constants denoted by the identifiers a, b, and c are > therefore of type int and must have size 4 (on a standard 32-bit system), > regardless of the size of the enumerated type aa. Thank you all for your replies. But I have another question to ask Consider another example #include int main() { enum ff { p = 0, q = 4294967295, r }; printf("size = %d %d %d\n", sizeof(enum ff),sizeof(q), sizeof(r)); printf("value= %d %d %d\n", p,q,r); } Its output is with default options on gcc : size = 8 8 8 value= 0 -1 0 Enumerator are supposed to be int and must have size 4. why are they being treated as long here and taking 8 bytes? Size of int on my machine is 4. Also the compiler doesn't even gives a warning when the tc is compiled. > > > > (Neither output is compliant to the standard, of course, as -fshort- > enums > > is a deviation from the standard.) > > Nope, the standard is entirely unambiguous: > > > 6.7.2.2 Enumeration specifiers > > 4 Each enumerated type shall be compatible with an integer type. The > choice > of type is > implementation-defined,97) but shall be capable of representing the values > of all the > members of the enumeration. The enumerated type is incomplete until after > the } that > terminates the list of enumerator declarations. > > > The choice of what integer type to use to store a value of an enumerated > type is implementation-defined, and if a char is big enough to represent > all > the values, the implementation is at liberty to use a char. > > > > cheers, > DaveK > -- > Can't think of a witty .sigline today
query: -fshort-enums is default on which targets ?
Hi, The description about the option -fshort-enums on gcc.gnu.org says, that "-fshort-enums is the default on some targets, as required by the platform ABI." 1) Can anyone tell me, on which all targets, this is default? 2) Or if anyone can tell me any document through which I can find the list of those target. 3) I believe that this can also be found out by looking into gcc source code. Can anyone help to by telling me the approximate section of code, where I should look for this? --gaurav