C语言 如何在C中打印无符号字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15736497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print an unsigned char in C?
提问by Anton
I am trying to print char as positive value:
我正在尝试将 char 打印为正值:
char ch = 212;
printf("%u", ch);
but I get:
但我得到:
4294967252
How I can get 212in the output?
我怎样才能进入212输出?
采纳答案by Anton
Declare your chas
声明你ch为
unsigned char ch = 212 ;
And your printf will work.
你的 printf 会起作用。
回答by dasblinkenlight
This is because in this case the chartype is signed on your system*. When this happens, the data gets sign-extended during the default conversions while passing the data to the function with variable number of arguments. Since 212 is greater than 0x80, it's treated as negative, %uinterprets the number as a large positive number:
这是因为在这种情况下,char类型是在您的系统上签名的*。发生这种情况时,数据会在默认转换期间进行符号扩展,同时将数据传递给具有可变数量参数的函数。由于 212 大于 0x80,它被视为负数,%u将数字解释为一个大的正数:
212 = 0xD4
When it is sign-extended, FFs are pre-pended to your number, so it becomes
当它被符号扩展时,FFs 被前置到你的号码,所以它变成
0xFFFFFFD4 = 4294967252
which is the number that gets printed.
这是打印的数字。
Note that this behavior is specific to your implementation. According to C99 specification, all chartypes are promoted to (signed) int, because an intcan represent all values of a char, signed or unsigned:
请注意,此行为特定于您的实现。根据 C99 规范,所有char类型都被提升为 (signed) int,因为 anint可以表示 a char、有符号或无符号的所有值:
6.1.1.2: If an
intcan represent all values of the original type, the value is converted to anint; otherwise, it is converted to anunsigned int.
6.1.1.2:如果an
int可以表示原始类型的所有值,则将该值转换为一个int;否则,它被转换为unsigned int。
This results in passing an intto a format specifier %u, which expects an unsigned int.
这导致将 an 传递int给格式说明符%u,该说明符需要一个unsigned int.
To avoid undefined behavior in your program, add explicit type casts as follows:
为避免程序中出现未定义的行为,请按如下方式添加显式类型转换:
unsigned char ch = (unsigned char)212;
printf("%u", (unsigned int)ch);
**一般而言,标准将签名留给
charchar实施。看this question这个问题了解更多详情。回答by Eric Postpischil
There are two bugs in this code. First, in most C implementations with signed char, there is an overflow in char ch = 212because 212 does not fit in an 8-bit signed char, and the C standard does not define behavior when there is integer overflow (it requires the implementation to define the behavior). It should instead be:
这段代码有两个错误。首先,在大多数带有 signed 的 C 实现中char,存在溢出,char ch = 212因为 212 不适合 8 位 signed char,并且 C 标准没有定义整数溢出时的行为(它需要实现来定义行为)。它应该是:
unsigned char ch = 212;
Second, in printf("%u",ch), chwill be promoted to an intin normal C implementations. However, the %uspecifier expects an unsigned int, and the C standard does not define behavior when the wrong type is passed. It should instead be:
其次,在printf("%u",ch),ch将被提升到一个int正常C实现。但是,说明%u符需要一个unsigned int,并且 C 标准不会定义传递错误类型时的行为。它应该是:
printf("%u", (unsigned) ch);
回答by Makketronix
In case you cannot change the declaration for whatever reason, you can do:
如果您因任何原因无法更改声明,您可以执行以下操作:
char ch = 212;
printf("%d", (unsigned char) ch);
回答by Shohanur Rahaman
Because charis by default signeddeclared that means the range of the variable is
因为char是默认signed声明的,这意味着变量的范围是
-127 to +127>
-127 到 +127>
your value is overflowed. To get the desired value you have to declared the unsignedmodifier. the modifier's (unsigned) range is:
你的价值溢出了。要获得所需的值,您必须声明unsigned修饰符。修饰符的 ( unsigned) 范围是:
0 to 255
to get the the range of any data type follow the process 2^bitexample charis 8 bit length to get its range just 2 ^(power) 8.
要获取任何数据类型的范围,请遵循流程2^bit示例char是 8 位长度以获取其范围2 ^(power) 8。
回答by banarun
The range of char is 127 to -128. If you assign 212, ch stores -44 (212-128-128) not 212.So if you try to print a negative number as unsigned you get (MAX value of unsigned int)-abs(number)which in this case is 4294967252
char 的范围是 127 到 -128。如果分配 212,则 ch 存储 -44 (212-128-128) 而不是 212。因此,如果您尝试将负数打印为无符号数,则会得到(无符号整数的最大值)-abs(number),在这种情况下为 4294967252
So if you want to store 212 as it is in ch the only thing you can do is declare ch as
因此,如果您想将 212 存储在 ch 中,您唯一可以做的就是将 ch 声明为
unsigned char ch;
now the range of ch is 0 to 255.
现在 ch 的范围是 0 到 255。

