#1025 A bug in the if() statement

closed-rejected
5
2013-05-25
2005-11-24
Anonymous
No

HI !

1. I am using SDCC 2.5.4 #1168 (Nov 23 2005)(MINGW32)
2. Target is 8051
3. I found a bug in the if() statement, it look like
a type casting bug, I don't know

4. Sample code is:

unsigned char ByteVar1; //8 bit variable
unsigned char ByteVar2; //8 bit variable

ByteVar1 = 0x00;
ByteVar2 = 0xFF;
if( ByteVar1 == (ByteVar2+1) )
{ printf( "It is OK");}
else { printf( "It may be a bug"); }

5. Could you help me !!!

6. My mail is yeechen@hmt.com.tw

THANKS !!!

Discussion

  • Bernhard Held

    Bernhard Held - 2005-11-24
    • assigned_to: nobody --> bernhardheld
    • status: open --> closed-rejected
     
  • Bernhard Held

    Bernhard Held - 2005-11-24

    Logged In: YES
    user_id=203539

    No it's not a bug, it's according to the C standard. The
    integer promotion rules require that ByteVar2 is promoted
    to int before the addition and comparison. What actually
    happens is:
    (int) ByteVar1 == (int) ByteVar2 + 1
    You can get your expected behaviour with:
    ByteVar1 == (unsigned char) (ByteVar2 + 1)

     
  • Nobody/Anonymous

    Logged In: NO

    TO: bernhardheld

    Thanks for your help

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks