/* This program shows the effect of undefined behavior on signed integers * with GCC (-INT_MIN overflows: the real value is not representable in * an int). Tests done with GCC 4.6.2. * * With no optimizations: * $ gcc int-overflow.c; ./a.out * 0 * * With -fstrict-overflow (enabled by -O2): * $ gcc -fstrict-overflow int-overflow.c; ./a.out * 1 * $ gcc -O2 int-overflow.c; ./a.out * 1 * * With -fwrapv, the behavior gets defined by GCC (the subtraction is * assumed to wrap around using 2's-complement representation). * $ gcc -O2 -fwrapv int-overflow.c; ./a.out * 0 * * $Id: int-overflow.c 47932 2011-12-01 13:37:18Z vinc17/ypig $ */ #include #include static int foo (int i) { return (i < 0 ? -i : i) >= 0; } int main (void) { printf ("%d\n", foo (INT_MIN)); return 0; }