I have seen the following code produce an unexpected result on two different
gcc compilers.  I compile the following code

#include <stdio.h>

int main()
{
        int a, d1, r1, r2;
        unsigned int d2;

        a = -10;
        d1 = 2;
        d2 = 3;

        r1 = a / d1;
        r2 = a / d2;

        printf( "r1 = %d\tr2 = %d\n", r1, r2 );

        return 0;
}

using

gcc -Wall sample_code.c 

I do not receive any warning messages.  When I run the code I get

% ./a.out 
r1 = -5 r2 = 1431655762

I think it would be nice to get a warning message on the second division.

Below is an example of one compiler showing the problem


Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5659~1/src/configure --disable-checking
--enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib
--build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10-
--host=x86_64-apple-darwin10 --target=i686-apple-darwin10
--with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5659)


-- 
           Summary: int = int / unsigned int yields unexpected result
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: okingsmith at nuvation dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44407

Reply via email to