如何在C中打印无符号的char ?

时间:2022-02-08 22:54:17

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?

如何得到212 ?

6 个解决方案

#1


32  

Declare your ch as

声明你的ch

unsigned char ch = 212 ;

And your printf will work.

你的printf将会工作。

#2


28  

This is because in this case the char type 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, %u interprets 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

当它是信号扩展的时候,ff会被提前到你的号码,所以它就变成了。

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类型都被提升为(签名)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; otherwise, it is converted to an unsigned int.

6.1.1.2:如果int可以表示原始类型的所有值,则将该值转换为int;否则,它将转换为无符号整数。

This results in passing an int to a format specifier %u, which expects an unsigned int.

这导致将int传递给格式说明符%u,它期望一个未签名的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. See this question for more details.

#3


10  

There are two bugs in this code. First, in most C implementations with signed char, there is an overflow in char ch = 212 because 212 does not fit in an 8-bit signed char, and the C standard does not define behavior when there is integer overflow. It should instead be:

这个代码中有两个bug。首先,在带有签名char的大多数C实现中,在char ch = 212中有一个溢出,因为212不适合8位签名的char,而C标准在有整数溢出时不定义行为。它应该是:

unsigned char ch = 212;

Second, in printf("%u",ch), ch will be promoted to an int in normal C implementations. However, the %u specifier 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将被提升到普通C实现中的int数。但是,%u说明符期望一个未签名的int类型,而C标准在传递错误类型时没有定义行为。它应该是:

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

#4


2  

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(数字),在本例中是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中,你唯一能做的就是声明为。

unsigned char ch;

now the range of ch is 0 to 255.

现在ch的范围是0到255。

#5


1  

In case you cannot change the declaration for whatever reason, you can do:

如果您无法更改声明,您可以这样做:

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

#6


1  

Because char is by default signed declared that means the range of the variable is

因为char是默认签署的,这意味着变量的范围是。

-127 to +127>

-127 + 127 >

your value is overflowed. To get the desired value you have to declared the unsigned modifier. the modifier's (unsigned) range is:

你的价值是溢出。要获得所需的值,必须声明未签名的修饰符。修改器的(未签名)范围为:

 0 to 255

to get the the range of any data type follow the process 2^bit example charis 8 bit length to get its range just 2 ^(power) 8.

得到任何数据类型的范围按照流程2 ^一点恩典示例8位长度范围2 ^(权力)8。

#1


32  

Declare your ch as

声明你的ch

unsigned char ch = 212 ;

And your printf will work.

你的printf将会工作。

#2


28  

This is because in this case the char type 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, %u interprets 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

当它是信号扩展的时候,ff会被提前到你的号码,所以它就变成了。

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类型都被提升为(签名)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; otherwise, it is converted to an unsigned int.

6.1.1.2:如果int可以表示原始类型的所有值,则将该值转换为int;否则,它将转换为无符号整数。

This results in passing an int to a format specifier %u, which expects an unsigned int.

这导致将int传递给格式说明符%u,它期望一个未签名的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. See this question for more details.

#3


10  

There are two bugs in this code. First, in most C implementations with signed char, there is an overflow in char ch = 212 because 212 does not fit in an 8-bit signed char, and the C standard does not define behavior when there is integer overflow. It should instead be:

这个代码中有两个bug。首先,在带有签名char的大多数C实现中,在char ch = 212中有一个溢出,因为212不适合8位签名的char,而C标准在有整数溢出时不定义行为。它应该是:

unsigned char ch = 212;

Second, in printf("%u",ch), ch will be promoted to an int in normal C implementations. However, the %u specifier 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将被提升到普通C实现中的int数。但是,%u说明符期望一个未签名的int类型,而C标准在传递错误类型时没有定义行为。它应该是:

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

#4


2  

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(数字),在本例中是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中,你唯一能做的就是声明为。

unsigned char ch;

now the range of ch is 0 to 255.

现在ch的范围是0到255。

#5


1  

In case you cannot change the declaration for whatever reason, you can do:

如果您无法更改声明,您可以这样做:

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

#6


1  

Because char is by default signed declared that means the range of the variable is

因为char是默认签署的,这意味着变量的范围是。

-127 to +127>

-127 + 127 >

your value is overflowed. To get the desired value you have to declared the unsigned modifier. the modifier's (unsigned) range is:

你的价值是溢出。要获得所需的值,必须声明未签名的修饰符。修改器的(未签名)范围为:

 0 to 255

to get the the range of any data type follow the process 2^bit example charis 8 bit length to get its range just 2 ^(power) 8.

得到任何数据类型的范围按照流程2 ^一点恩典示例8位长度范围2 ^(权力)8。