Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #150280
| Newsgroups | comp.lang.c |
|---|---|
| Date | 2020-02-27 01:29 -0800 |
| References | <aaf0f57f-4ae1-4c4a-a0aa-314eade48440@googlegroups.com> <r35hab$ts9$1@dont-email.me> <5c585469-72c8-42f8-9e74-848f160db602@googlegroups.com> |
| Message-ID | <aafdcd8f-ba2e-4237-9df9-c9808a34b78f@googlegroups.com> (permalink) |
| Subject | Re: clarification about enums in C |
| From | already5chosen@yahoo.com |
On Thursday, February 27, 2020 at 12:05:12 AM UTC+2, already...@yahoo.com wrote:
> On Wednesday, February 26, 2020 at 12:29:40 PM UTC+2, David Brown wrote:
> > On 26/02/2020 00:38, Thiago Adams wrote:
> > > From the standard:
> > >
> > > "The identifiers in an enumerator list are declared as constants
> > > that have type int and may appear wherever such are permitted.127)"
> > >
> > > But there is a foot note:
> > >
> > > "Each enumerated type shall be compatible with char, a signed
> > > integer type, or an unsigned integer type. The choice of type
> > > is implementation-defined,128)"
> > >
> > > "128) An implementation may delay the choice of which integer
> > > type until all enumeration constants have been seen."
> > >
> > > I am confused.
> > >
> >
> > You are not the first person to feel that way.
> >
> > > enum E { A, B};
> >
> > The key weirdness of "enum" in C is that "enum E" is a type, but A and B
> > are not objects of that type. They are, however, members of the
> > enumeration "E".
> >
> > "A" and "B" are named integer constant values (much like 'a' and 'b').
> > So they have type "int".
> >
>
> That' how language of the standard sounds.
> But it's not how gcc and clang work.
>
> enum enum_t {
> uns = (unsigned)-1,
> neg = -1,
> };
> Both in gcc and in clang uns has type 'unsigned int' rather than 'int'.
> And indeed it differs from type of 'enum_t' which in ths case is 'int'.
>
> Microsoft's compiler (VS2015) is easier to understand and don't violate (my understanding of) Standard, but it is less flexible - everything, including uns, is 'int'.
>
>
> > "enum E" is an enumerated type. It is an odd thing - it is an integer
> > type (6.2.5p17), and it is required to be compatible with a char, a
> > signed integer type, or an unsigned integer type (6.7.2.2p4). I am sure
> > it was not intended that this requirement be recursive here!
> >
> > It is up to the compiler to decide which integer type "enum E" should be
> > compatible with. Once it is picked (gcc picks "unsigned int" here,
> > unless at least one of the enumerators is negative, when it picks
> > "int"), the type "enum E" is indistinguishable from that type. So
> > _Generic() on gcc would match "enum E" as "unsigned int". As far as I
> > understand it (and I could be wrong), "enum E" is therefore a new type
> > that is in every way identical and compatible to an existing type - but
> > is not an alias for it (like a typedef).
> >
> > (Of course, compilers and other tools can track a distinction here, and
> > use it for extra warnings or analysis.)
> >
> > >
> > > Can A and B be int and E not int?
> >
> > A and B are always "int". E might not be.
> >
> > (Many compilers have extensions to allow A and B to be "long int".)
> >
In case of Windows port of gcc and clang, even 'long long int',
This case is especially funny:
enum en_t {
A = 1,
B = -1,
C = (unsigned)-1,
};
type of en_t is 'long long'
A == 1, type is either 'int' or 'unsigned', I can't tell for sure
B == -1, type is 'int
C == 2**32-1, type is 'long long' or 'unsigned long long', I can't tell for sure.
Again, that's on Windows/Msys2.
On Linux, I wouldn't know how to distinguish between rvalues of types 'long' and 'long long'.
> > > Can A B and E be != int?
> > >
> > > If so, what is this
> > >
> > > "The identifiers in an enumerator list are declared as constants
> > > that have type int and may appear wherever such are permitted.127)"
> > >
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
clarification about enums in C Thiago Adams <thiago.adams@gmail.com> - 2020-02-25 15:38 -0800
Re: clarification about enums in C luser droog <luser.droog@gmail.com> - 2020-02-25 19:19 -0800
Re: clarification about enums in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-25 21:17 -0800
Re: clarification about enums in C Thiago Adams <thiago.adams@gmail.com> - 2020-02-27 04:01 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-26 11:29 +0100
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-26 14:05 -0800
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-26 23:03 +0000
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-26 23:29 -0800
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 01:42 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 11:02 +0100
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-27 06:37 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 16:18 +0100
Re: clarification about enums in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-27 23:24 -0500
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 09:50 +0100
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-27 06:33 -0800
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 06:53 -0800
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 15:05 +0000
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 16:24 +0100
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-28 01:27 -0800
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 01:29 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 11:12 +0100
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 03:09 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 14:33 +0100
Re: clarification about enums in C already5chosen@yahoo.com - 2020-02-27 05:44 -0800
Re: clarification about enums in C David Brown <david.brown@hesbynett.no> - 2020-02-27 14:57 +0100
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-27 11:53 +0000
Re: clarification about enums in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-27 23:52 -0500
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-28 01:30 -0800
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-26 14:09 +0000
Re: clarification about enums in C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-02-26 09:47 -0800
Re: clarification about enums in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-26 21:04 +0000
csiph-web