如何使用%c输出打印577”?
#include<stdio.h>
int main()
{
int i = 577;
printf("%c",i);
return 0;
}
编译后,其给出输出“ A”。谁能解释我如何得到这个?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
#include<stdio.h>
int main()
{
int i = 577;
printf("%c",i);
return 0;
}
编译后,其给出输出“ A”。谁能解释我如何得到这个?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
%c
只能接受最多包括255个值,然后它将从0开始!577%256 = 65; //('a'的char代码)
%c
will only accept values up to 255 included, then it will start from 0 again !577 % 256 = 65; // (char code for 'A')
这与值转换的方式有关。
%c
格式指定符期望int
参数,然后将其转换为type无符号char
。然后编写结果未签名的char
的字符。printf 的格式指定符,以下有关
c
:当将值转换为较小的未签名类型时,有效发生的是较高的字节被截断,较低的字节具有结果值。
第6.3.1.3p2节有关整数转换状态:
当使用两个补体表示时,这与截断高阶字节相同。
对于
int
值577,其在十六进制中的值为0x241,低顺序字节为0x41或DECIMAL 65。在ASCII中,此代码是字符a
a 是打印的内容。This has to do with how the value is converted.
The
%c
format specifier expects anint
argument and then converts it to typeunsigned char
. The character for the resultingunsigned char
is then written.Section 7.21.6.1p8 of the C standard regarding format specifiers for
printf
states the following regardingc
:When converting a value to a smaller unsigned type, what effectively happens is that the higher order bytes are truncated and the lower order bytes have the resulting value.
Section 6.3.1.3p2 regarding integer conversions states:
Which, when two's complement representation is used, is the same as truncating the high-order bytes.
For the
int
value 577, whose value in hexadecimal is 0x241, the low order byte is 0x41 or decimal 65. In ASCII this code is the characterA
which is what is printed.使用
printf()
。“%c”
匹配int
参数*1 。int
值将转换为无符号char
65的值和相应的字符*2 ,'a'a'
然后打印。如果A
char
是 或 unsigned 或用2的补充编码。没有不确定的行为(ub)。在堆栈,注册或....int
的末日是无关的,这是没有区别的。参数值将转换为无符号char
,并打印相应的字符。*1 允许所有
int
值[int_min ... int_max]
。当
char
值以...
参数传递时,首先将其转换为int
,然后传递。*2 65是ASCII
a
,字符的无处不在编码。很少使用其他编码。With
printf()
."%c"
matches anint
argument*1. Theint
value is converted to anunsigned char
value of 65 and the corresponding character*2,'A'
is then printed.This makes no difference if a
char
is signed or unsigned or encoded with 2's complement or not. There is no undefined behavior (UB). It makes no difference how the argument is passed, on the stack, register, or .... The endian ofint
is irrelevant. The argument value is converted to anunsigned char
and the corresponding character is printed.*1All
int
values are allowed[INT_MIN...INT_MAX]
.When a
char
value is passed as...
argument, it is first converted to anint
and then passed.*2 65 is an ASCII
A
, the ubiquitous encoding of characters. Rarely other encodings are used.只需在十六进制表示中输出变量
i
的。
值 代码>'a'。
Just output the value of the variable
i
in the hexadecimal representationThe program output will be
So the least significant byte contains the hexadecimal value
0x41
that represents the ASCII code of the letter'A'
.HEX中的577为0x241。
'a'
的ASCII表示为0x41。您将int
传递给printf
,然后告诉printf
将其视为char> char
(因为<代码>%c )。char
是单字节宽,因此printf
查看您给它的第一个参数,并读取最不重要的字节,即0x41。要打印整数,您需要使用
%d
或%i
。577 in hex is 0x241. The ASCII representation of
'A'
is 0x41. You're passing anint
toprintf
but then tellingprintf
to treat it as achar
(because of%c
). Achar
is one-byte wide and soprintf
looks at the first argument you gave it and reads the least significant byte which is 0x41.To print an integer, you need to use
%d
or%i
.