[英]Why does printf( “%c”, 1) return smiley face instead of coded char for 1
This is my code 这是我的代码
#include <stdio.h>
int x,y;
int main( void )
{
for ( x = 0; x < 10; x++, printf( "\n" ) )
for ( y = 0; y < 10; y++ )
printf( "%c", 1 );
return 0;
}
It returns smiley faces. 它会回归笑脸。 I searched everywhere for a code for smiley face or a code for 1 but I didn't manage to find any links whatsoever or any explanation why char value for 1 returns smiley face, when the ascii code for 1 is SOH.
我到处搜索笑脸的代码或1的代码但我没有设法找到任何链接或任何解释为什么1的char值返回笑脸,当1的ascii代码是SOH时。 I researched answers for this question but I didn't find any answers that explain why this happens.
我研究了这个问题的答案,但我没有找到解释为什么会发生这种情况的答案。
The output varies among different terminals. 输出在不同终端之间变化。 For example, on my OS X default terminal, no characters are output.
例如,在我的OS X默认终端上,不输出任何字符。
In your case, ☺
is output presumably due to some historical reasons . 在你的情况下,
☺
的输出可能是由于某些历史原因 。 In short, this is because code page 437, which maps byte 0x01
to U+263A
, is the character set of MS-DOS. 简而言之,这是因为将字节
0x01
映射到U+263A
代码页437是MS-DOS的字符集。
Because 1
isn't a printable character code. 因为
1
不是可打印的字符代码。 If you want '1'
you need to write it with the character literal: 如果你想要
'1'
你需要用字符文字来写它:
printf( "%c", '1' );
// ^^^
If you use a number, you'll select a character number from ASCII table, if you use a char
you'll find the char. 如果使用数字,则从ASCII表中选择一个字符编号,如果使用
char
,则可以找到字符。
Example: this code prints the ASCII character number 65: 示例:此代码打印ASCII字符编号65:
printf( "%c", 65 ); // Outputs: A
This code prints the letter A: 此代码打印字母A:
printf( "%c", 'A' ); // Outputs: A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.