如何使用%c输出打印577”?

发布于 2025-02-12 03:16:10 字数 178 浏览 1 评论 0 原文

#include<stdio.h>
int main()
{
  int i = 577;
  printf("%c",i);
  return 0;
}

编译后,其给出输出“ A”。谁能解释我如何得到这个?

#include<stdio.h>
int main()
{
  int i = 577;
  printf("%c",i);
  return 0;
}

After compiling, its giving output "A". Can anyone explain how i'm getting this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫腻 2025-02-19 03:16:10

%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')

一个人的旅程 2025-02-19 03:16:10

这与值转换的方式有关。

%c 格式指定符期望 int 参数,然后将其转换为type 无符号char 。然后编写结果未签名的char 的字符。

printf 的格式指定符,以下有关 c

如果没有 l 长度修饰符,则 int 参数将转换为一个
unsigned char ,并编写了结果字符。

当将值转换为较小的未签名类型时,有效发生的是较高的字节被截断,较低的字节具有结果值。

第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 an int argument and then converts it to type unsigned char. The character for the resulting unsigned char is then written.

Section 7.21.6.1p8 of the C standard regarding format specifiers for printf states the following regarding c:

If no l length modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written.

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:

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or
subtracting one more than the maximum value that can be represented in the new type
until the value is in the range of the new type.

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 character A which is what is printed.

我不会写诗 2025-02-19 03:16:10

用%c输出“ a”打印577?

使用 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 ,然后传递。

char ch = 'A';
printf("%c", ch); // ch is converted to an `int` and passed to printf().

*2 65是ASCII a ,字符的无处不在编码。很少使用其他编码。

How does printing 577 with %c output "A"?

With printf(). "%c" matches an int argument*1. The int value is converted to an unsigned 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 of int is irrelevant. The argument value is converted to an unsigned 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 an int and then passed.

char ch = 'A';
printf("%c", ch); // ch is converted to an `int` and passed to printf().

*2 65 is an ASCII A, the ubiquitous encoding of characters. Rarely other encodings are used.

ヅ她的身影、若隐若现 2025-02-19 03:16:10

只需在十六进制表示中输出变量 i

#include <stdio.h>

int main( void )
{
    int i = 577;

    printf( "i = %#x\n", i );
}

i = 0x241

值 代码>'a'。

Just output the value of the variable i in the hexadecimal representation

#include <stdio.h>

int main( void )
{
    int i = 577;

    printf( "i = %#x\n", i );
}

The program output will be

i = 0x241

So the least significant byte contains the hexadecimal value 0x41 that represents the ASCII code of the letter 'A'.

仄言 2025-02-19 03:16:10

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 an int to printf but then telling printf to treat it as a char (because of %c). A char is one-byte wide and so printf 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文