Re: Inconsistency with C language standard

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2021-10-14 at 07:38 +0100, Jonathan Wakely via Gcc-help wrote:
> On Thu, 14 Oct 2021, 03:29 3497650172 via Gcc-help,
> <gcc-help@xxxxxxxxxxx>
> wrote:
> 
> > The C language standard specifies that the "enum" constant is of type
> > int,
> > but GCC supports all integers.And i did not find this discrepancy in
> > the
> > official manual.
> 
> 
> There is not discrepancy, GCC conforms to the standard. It is documented
> at
> https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html
> 
> Did I not find it or did the official website document not explain it at
> > all?
> 
> 
> You didn't find it.
> 
> How do I know all the differences?
> 
> 
> Read the manual
> https://gcc.gnu.org/onlinedocs/gcc/C-Implementation.html

Hi Jonathan,

I think the OP is refering to C99 6.4.4.3p2:

> An identifier declared as an enumeration constant has type int.

There is some tricky difference between the concepts "the type of an
enumeration constant" and "the type compatible to an enumeration type".

However, the standard (C99, 6.7.2.2 p2) also says:

> The expression that defines the value of an enumeration constant shall be an integer
> constant expression that has a value representable as an int.

So if you specify a value which doesn't fit in an int, you're invoking
an undefined behavior.  For example:

enum { x = 1145141919810L };

invokes undefined behavior and the compiler can do anything. "anything"
includes chosing another type for x, and/or even worse.

With -Wpedantic:

> t.c:4:12: warning: ISO C restricts enumerator values to range of 'int'
> [-Wpedantic]
>     4 | enum { x = 1145141919810L };
>       |            ^~~~~~~~~~~~~~
-- 
Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux