This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Is this a bug for KEIL C?

Create a simple project, use 8051 chip;

input a simple program

void main(void) //1
{ //2 char a,b; //3 a=-1; //4 b=a/2; //5 while(1); //6
}

build and debug step by step
Open watch window,notice local variable
when it executed at the 6th line.
varvariable b should be 0.
but you will see arvariable b is -1 in the watch window

Parents
  • No, it's not a bug at all.

    If you get out your copy of K&R and look at the chapter about arithmetic operators, it say that the direction of truncation for the division operator is machine-dependent for negative operands.

    This means that the result of (-1 / 2) can be 0 or -1 depending on the hardware and/or the compiler. Neither of the results would be considered an error of the compiler.

    If you need a certain direction when truncating the result of dividing a negative number, you will have to do so by hand.

Reply
  • No, it's not a bug at all.

    If you get out your copy of K&R and look at the chapter about arithmetic operators, it say that the direction of truncation for the division operator is machine-dependent for negative operands.

    This means that the result of (-1 / 2) can be 0 or -1 depending on the hardware and/or the compiler. Neither of the results would be considered an error of the compiler.

    If you need a certain direction when truncating the result of dividing a negative number, you will have to do so by hand.

Children
No data