Simple Character Interpretation In C

2019-02-15 06:24发布

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?

标签: c format char
9条回答
混吃等死
2楼-- · 2019-02-15 06:32

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.

查看更多
虎瘦雄心在
3楼-- · 2019-02-15 06:35

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, but char 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:

#include <stdint.h>
...
uint8_t ch = 129;

As a rule of thumb, make sure to follow these rules in MISRA-C:2004:

6.1 The plain char type shall be used only for the storage and use of character values.

6.2 signed and unsigned char type shall be used only for the storage and use of numeric values.

查看更多
走好不送
4楼-- · 2019-02-15 06:37

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

查看更多
Bombasti
5楼-- · 2019-02-15 06:38

The type char can be either signed or unsigned, 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.

查看更多
老娘就宠你
6楼-- · 2019-02-15 06:40

It means you ran into undefined behavior.

Any outcome is possible.

char ch=129; is UB because 129 is not a representable value for a char for you specific setup.

查看更多
仙女界的扛把子
7楼-- · 2019-02-15 06:40

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.

查看更多
登录 后发表回答