This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
[gcc] no warning on int conversion overflow in non-c99 mode
- From: A A <wempwer at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 17 Feb 2014 15:34:58 +0100
- Subject: [gcc] no warning on int conversion overflow in non-c99 mode
- Authentication-results: sourceware.org; auth=none
The warning about overflows in int conversions show up only when
std=99 option is used. In non-c99 mode, a different warning shows
up. However, warning about char overflow shows up in both. Examples
cat << EOF | gcc -c -xc -pedantic -std=c99 -
> char foo = 255;
> signed int ia = 2166136261;
> signed int ia1 = 2147483648;
> EOF
<stdin>:1:1: warning: overflow in implicit constant conversion [-Woverflow]
<stdin>:2:1: warning: overflow in implicit constant conversion [-Woverflow]
<stdin>:3:1: warning: overflow in implicit constant conversion [-Woverflow]
cat << EOF | gcc -c -xc -pedantic -
> char foo = 255;
> signed int ia = 2166136261;
> signed int ia1 = 2147483648;
> EOF
<stdin>:1:1: warning: overflow in implicit constant conversion [-Woverflow]
<stdin>:2:1: warning: this decimal constant is unsigned only in ISO
C90 [enabled by default]
<stdin>:3:1: warning: this decimal constant is unsigned only in ISO
C90 [enabled by default]
It looks like in case of int gcc simply compares if lvalue has the
same size as rvalue. Integer constants conversion rules have changed
between C90 and C99. In C90, 2166136261 is unsigned long int compiler
which has size of 4, the same as int hence no warning. But in C99
2166136261 is long long int which is 8 bytes. OTOH, if `u' suffix is
add to integer constant making it unsigned integer no warning is
generated in both modes:
cat << EOF | gcc -c -xc -pedantic -The warning about overflows in int
conversions show up only when
std=99 option is used. In non-c99 mode, a different warning shows
up. However, warning about char overflow shows up in both. Examples
cat << EOF | gcc -c -xc -pedantic -std=c99 -
> char foo = 255;
> signed int ia = 2166136261;
> signed int ia1 = 2147483648;
> EOF
<stdin>:1:1: warning: overflow in implicit constant conversion [-Woverflow]
<stdin>:2:1: warning: overflow in implicit constant conversion [-Woverflow]
<stdin>:3:1: warning: overflow in implicit constant conversion [-Woverflow]
cat << EOF | gcc -c -xc -pedantic -
> char foo = 255;
> signed int ia = 2166136261;
> signed int ia1 = 2147483648;
> EOF
<stdin>:1:1: warning: overflow in implicit constant conversion [-Woverflow]
<stdin>:2:1: warning: this decimal constant is unsigned only in ISO
C90 [enabled by default]
<stdin>:3:1: warning: this decimal constant is unsigned only in ISO
C90 [enabled by default]
It looks like in case of int gcc simply compares if lvalue has the
same size as rvalue. Integer constants conversion rules have changed
between C90 and C99. In C90, 2166136261 is unsigned long int compiler
which has size of 4, the same as int hence no warning. But in C99
2166136261 is long long int which is 8 bytes. OTOH, if `u' suffix is
add to integer constant making it unsigned integer no warning is
generated in both modes:
cat << EOF | gcc -c -xc -pedantic -
> signed int ia = 2166136261u;
> signed int ia1 = 2147483648u;
> EOF
cat << EOF | gcc -c -xc -pedantic -std=c99 -
> signed int ia = 2166136261u;
> signed int ia1 = 2147483648u;
> EOF
> signed int ia = 2166136261u;
> signed int ia1 = 2147483648u;
> EOF
cat << EOF | gcc -c -xc -pedantic -std=c99 -
> signed int ia = 2166136261u;
> signed int ia1 = 2147483648u;
> EOF