Here is my code
#include<stdio.h>
void main()
{
char ch = 129;
printf("%d", ch);
}
I get the output as -127. What does it mean?
Here is my code
#include<stdio.h>
void main()
{
char ch = 129;
printf("%d", ch);
}
I get the output as -127. What does it mean?
Your
char
is most likely an 8-bit signed integer that is stored using Two's complement. Such a variable can only represent numbers between -128 and 127. If you do "127+1" it wraps around to -128. So 129 is equivalent to -127.Whether a plain
char
is signed or unsigned, is implementation-defined behavior. This is a quite stupid, obscure rule in the C language.int
,long
etc are guaranteed to be signed, butchar
could be signed or unsigned, it is up to the compiler implementation.On your particular compiler,
char
is apparently signed. This means, assuming that your system uses two's complement, that it can hold values of -128 to 127.You attempt to store the value 129 in such a variable. This leads to undefined behavior, because you get an integer overflow. Strictly speaking, anything can happen when you do this. The program could print "hello world" or start shooting innocent bystanders, and still conform to ISO C. In practice, most (all?) compilers will however implement this undefined behavior as "wrap around", as described in other answers.
To sum it up, your code relies on two different behaviors that aren't well defined by the standard. Understanding how the result of such unpredictable code ends up in a certain way has limited value. The important thing here is to recognize that the code is obscure, and learn how to write it in a way that isn't obscure.
The code could for example be rewritten as:
unsigned char ch = 129;
Or even better:
As a rule of thumb, make sure to follow these rules in MISRA-C:2004:
This comes from the fact that a
char
is coded on one byte, so 8 bits of data.In fact
char
has a value coded on 7 bits and have one bit for the sign,unsigned char
have 8 bits of data for its value.This means:
Taking abcdefgh as 8 bits respectively (a being the leftmost bit, and h the rightmost), the value is encoded with a for the sign and bcdefgh in binary format for the real value:
42(decimal) = 101010(binary) stored as : abcdefgh 00101010
When using this value from the memory : a is 0 : the number is positive, bcdefgh = 0101010 : the value is 42
What happens when you put 129 :
129(decimal) = 10000001(binary) stored as : abcdefgh 10000001
When using this value from the memory : a is 0 : the number is negative, we should substract one and invert all bits in the value, so (bcdefgh - 1) inverted = 1111111 : the value is 127 The number is -127
The type
char
can be eithersigned
orunsigned
, it's up to the compiler. Most compilers have it as `signed.In your case, the compiler silently converts the integer 129 to its signed variant, and puts it in an 8-bit field, which yields -127.
It means you ran into undefined behavior.
Any outcome is possible.
char ch=129;
is UB because129
is not a representable value for achar
for you specific setup.The
char
type is a 8-bit signed integer. If you interpret the representation of unsigned byte 129 in the two's complement signed representation, you get -127.