This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Interesting bug in number size determination


From: Szekeres Istvan <szekeres@cyberspace.mht.bme.hu>
>
>If I compile this program with -Wall:
>
> unsigned long u;
>
> u  = 4294967295; 
> printf( "%lld\n", u );
>
> u = 0xffffffff;
> printf("%lld\n", u);
>
>the compiler says: 
>a.c:4: warning: decimal constant is so large that it is unsigned
>(this is for u  = 4294967295). 
>Shouldn't it fit into an unsigned long?

Yes, but bare numeric literals are signed by default. The compiler is
warning you that it's doing a signed-to-unsigned conversion, which may
not have the results you expect if unsigned long has more than 32 bits.
Add "UL" or "ul" to the end of the number.

>The other interesting thing is that 0xffffffff=4294967295 and it doesn't
>have problems with the hex format :)

The absence of the warning in this case sounds like a genuine bug to
me, albeit a very minor one.

BTW, you're using the wrong print format -- "%lld" is signed long long;
you want "%lu" for unsigned long. EGCS warned about this when I tried
your code.

--
Ross Smith ................................... mailto:ross.s@ihug.co.nz
.............. The Internet Group, Auckland, New Zealand ..............
    "Perl is the Unix way. 500 million ways of doing the same thing,
    and 500 million monster egos all insisting on their way being
    the Proper way of doing it." -- David Parsons




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]