public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Inconsistency with C language standard
@ 2021-10-14  2:28 3497650172
  2021-10-14  6:38 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: 3497650172 @ 2021-10-14  2:28 UTC (permalink / raw)
  To: gcc-help

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.Did I not find it or did the official website document not explain it at all?How do I know all the differences?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Inconsistency with C language standard
  2021-10-14  2:28 Inconsistency with C language standard 3497650172
@ 2021-10-14  6:38 ` Jonathan Wakely
  2021-10-14 10:46   ` Xi Ruoyao
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2021-10-14  6:38 UTC (permalink / raw)
  To: 3497650172; +Cc: gcc-help

On Thu, 14 Oct 2021, 03:29 3497650172 via Gcc-help, <gcc-help@gcc.gnu.org>
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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Inconsistency with C language standard
  2021-10-14  6:38 ` Jonathan Wakely
@ 2021-10-14 10:46   ` Xi Ruoyao
       [not found]     ` <tencent_7A3E0B9A44D54C42051CA77EC71741F55005@qq.com>
  0 siblings, 1 reply; 4+ messages in thread
From: Xi Ruoyao @ 2021-10-14 10:46 UTC (permalink / raw)
  To: Jonathan Wakely, 3497650172; +Cc: gcc-help

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@gcc.gnu.org>
> 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@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: 回复: Inconsistency with C language standard
       [not found]     ` <tencent_7A3E0B9A44D54C42051CA77EC71741F55005@qq.com>
@ 2021-10-14 15:24       ` Xi Ruoyao
  0 siblings, 0 replies; 4+ messages in thread
From: Xi Ruoyao @ 2021-10-14 15:24 UTC (permalink / raw)
  To: 3497650172; +Cc: gcc-help

On Thu, 2021-10-14 at 20:58 +0800, 3497650172 wrote:
> In Visual Studio, if the value of enumeration constant exceeds the
> range that int can represent, hermit conversion will be performed,
> such as the following code:
> ————————————————————————
> #include<stdio.h>
> 
> enum A {
> 	MAX= 4294967295
> };
> int main() {
> 	if (MAX < 1) {
> 		printf("test");
> 	}
> }
> ————————————————————————
> In Visual Studio, the value of Max is - 1, so "test" will be output.
> but in GCC, the value of Max is 4294967295, so "test" will not be
> output.
> I think the Visual Studio is consistent with the C language standard,
> but GCC is inconsistent with the standard.

As I've explained, you are invoking undefined behavior, so a conforming
compiler can do anything.

The standard is not "you think".

-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-14 15:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-14  2:28 Inconsistency with C language standard 3497650172
2021-10-14  6:38 ` Jonathan Wakely
2021-10-14 10:46   ` Xi Ruoyao
     [not found]     ` <tencent_7A3E0B9A44D54C42051CA77EC71741F55005@qq.com>
2021-10-14 15:24       ` 回复: " Xi Ruoyao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).