简体   繁体   English

如何在 C 中打印无符号字符?

[英]How to print an unsigned char in C?

I am trying to print char as positive value:我正在尝试将 char 打印为正值:

char ch = 212;
printf("%u", ch);

but I get:但我得到:

4294967252

How I can get 212 in the output?如何在 output 中获得212

Declare your ch as将您的ch声明为

unsigned char ch = 212 ;

And your printf will work.你的 printf 会起作用。

This is because in this case the char type is signed on your system * .这是因为在这种情况下, char类型是在您的系统上签名的* 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, %u interprets the number as a large positive number:由于 212 大于 0x80,它被视为负数, %u将该数字解释为一个大的正数:

212 = 0xD4

When it is sign-extended, FF s are pre-pended to your number, so it becomes当它被符号扩展时, FF s 被前置到你的号码,所以它变成

0xFFFFFFD4 = 4294967252

which is the number that gets printed.这是打印的数字。

Note that this behavior is specific to your implementation.请注意,此行为特定于您的实现。 According to C99 specification, all char types are promoted to (signed) int , because an int can represent all values of a char , signed or unsigned:根据 C99 规范,所有char类型都被提升为 (signed) int ,因为int可以表示char所有值,有符号或无符号:

6.1.1.2: If an int can represent all values of the original type, the value is converted to an int ; 6.1.1.2:如果一个int可以表示原始类型的所有值,则将该值转换为int otherwise, it is converted to an unsigned int .否则,它被转换为unsigned int

This results in passing an int to a format specifier %u , which expects an unsigned int .这导致将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);


* In general, the standard leaves the signedness of char up to the implementation. *通常,标准将char的签名留给实现。 See this question for more details. 有关更多详细信息,请参阅此问题

There are two bugs in this code.这段代码有两个错误。 First, in most C implementations with signed char , there is a problem in char ch = 212 because 212 does not fit in an 8-bit signed char , and the C standard does not fully define the behavior (it requires the implementation to define the behavior).首先,在大多数带有 signed char C 实现中, char ch = 212存在问题,因为 212 不适合 8 位有符号char ,并且 C 标准没有完全定义行为(它需要实现来定义行为)。 It should instead be:它应该是:

unsigned char ch = 212;

Second, in printf("%u",ch) , ch will be promoted to an int in normal C implementations.其次,在printf("%u",ch)ch将在普通 C 实现中提升为int However, the %u specifier expects an unsigned int , and the C standard does not define behavior when the wrong type is passed.但是, %u说明符需要一个unsigned int ,并且 C 标准没有定义传递错误类型时的行为。 It should instead be:它应该是:

printf("%u", (unsigned) ch);

In case you cannot change the declaration for whatever reason, you can do:如果您因任何原因无法更改声明,您可以执行以下操作:

char ch = 212;
printf("%d", (unsigned char) ch);

The range of char is 127 to -128. char 的范围是 127 到 -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如果分配 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。

Because char is by default signed declared 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 unsigned modifier.要获得所需的值,您必须声明unsigned修饰符。 the modifier's ( unsigned ) range is:修饰符( unsigned )的范围是:

 0 to 255

to get the the range of any data type follow the process 2^bit example char is 8 bit length to get its range just 2 ^(power) 8 .要获取任何数据类型的范围,请按照过程2^bit示例char为 8 位长度以使其范围仅为2 ^(power) 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM