I know that signed int overflow is undefined in C.
I understood this as the resulting number is not predictable, but it is still a number.
However, this code does not behave like the result of an overflow operation is an int number:
#include<stdio.h>
int main ()
{
int i,m=-727379968,w=1,n;
for (i=1;i<=12;i++)
w*=10;
n=w;
printf("sizeof(int)=%d\n",(int)sizeof(int));
printf ("m=%d n=%d m-n=%d (1<m)=%d (1<n)=%d (m==n)=%d \n", m,n, m-n, 1<m, 1<n,m==n);
return (0);
}
Using gcc version 11.4.0 no optimization:
sizeof(int)=4
m=-727379968 n=-727379968 m-n=0 (1<m)=0 (1<n)=0 (m==n)=1
This is OK.
With gcc -O2:
sizeof(int)=4
m=-727379968 n=-727379968 m-n=0 (1<m)=0 (1<n)=1 (m==n)=0
This is wrong.
Why is 1 less than a negative number?
Why are m and n not equal when the difference is 0?
I expected that after the assignment n=w, there is some int value in n. But I do not understand the results.
Edit: Using gcc 14.2.0 (both with and without -O2) gives the correct result:
sizeof(int)=4
m=-727379968 n=-727379968 m-n=0 (1<m)=0 (1<n)=0 (m==n)=1
So it seems there was a bug in gcc 11.4.0.
O.K. we call it a bug if we expect undefined value. If we admitted undefined behavior we would not call it a bug.
wis undefined, so willnbe.