-2
#include<stdio.h>
int main()
{
 char c = 125;
 c = c+10;
 printf("%d", c);
 return 0;

}

Can any one please tell me why the output of the program is -121. In advance thank you

  • 1
    When treated as a signed value, a char has the range -128 to 127. When you added 10, it "overflowed" and became a negative value. – Marker Jul 08 '18 at 11:44
  • 1
    Possible duplicate of [C- why char c=129 will convert into -127?](https://stackoverflow.com/questions/20756626/c-why-char-c-129-will-convert-into-127) – Achal Jul 08 '18 at 11:51
  • Also you want to see this https://stackoverflow.com/questions/47263865/is-signed-char-overflow-undefined-within-the-range-255-to-255#comment-81478565 – Achal Jul 08 '18 at 11:53

1 Answers1

0

It happens because of integer overflow. Max char value is 127, the result of sum is 135 which is more

Alex Riabov
  • 8,655
  • 5
  • 47
  • 48