使用 printf 打印一个字符
这两个代码是否相同
char ch = 'a';
printf("%d", ch);
会打印垃圾值吗?
我对此感到困惑,
printf("%d", '\0');
这会打印 0 还是垃圾值? 因为当我这样做时,
printf("%d", sizeof('\n'));
它会打印 4。为什么 sizeof('\n')
是 4 个字节? C++ 中同样的事情打印 1 个字节。这是为什么?
所以c语言中的主要问题
是printf("%d", '\0')
应该打印0,
而在C++中printf("%d", '\0')
应该打印垃圾吗?
Are both these codes the same
char ch = 'a';
printf("%d", ch);
Will it print a garbage value?
I am confused about this
printf("%d", '\0');
Will this print 0 or garbage value?
Because when i do this
printf("%d", sizeof('\n'));
It prints 4. Why is sizeof('\n')
4 bytes?
The same thing in C++ prints 1 bytes. Why is that?
So here's the main question
in c language is printf("%d", '\0')
supposed to print 0
and in C++ printf("%d", '\0')
supposed to print garbage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
%d
打印一个整数:它将打印你的字符的 ascii 表示形式。您需要的是%c
:printf("%d", '\0');
打印'\0'
的 ascii 表示,即 0(通过转义 0,您告诉编译器使用 ascii 值 0。printf("%d", sizeof('\n'));
打印 4,因为字符文字是int
,C 语言,而不是char
。%d
prints an integer: it will print the ascii representation of your character. What you need is%c
:printf("%d", '\0');
prints the ascii representation of'\0'
, which is 0 (by escaping 0 you tell the compiler to use the ascii value 0.printf("%d", sizeof('\n'));
prints 4 because a character literal is anint
, in C, and not achar
.这应该打印字符的 ASCII 值,因为
%d
是整数的转义序列。因此,作为printf
参数给出的值在打印时被视为整数。对于
printf("%d", '\0');
也是如此,其中 NULL 字符被解释为 0 整数。最后,
sizeof('\n')
为 4,因为在 C 中,字符的这种表示法代表相应的 ASCII 整数。所以 '\n' 与整数 10 相同。这完全取决于您对字节的解释。
This is supposed to print the ASCII value of the character, as
%d
is the escape sequence for an integer. So the value given as argument ofprintf
is taken as integer when printed.Same holds for
printf("%d", '\0');
, where the NULL character is interpreted as the 0 integer.Finally,
sizeof('\n')
is 4 because in C, this notation for characters stands for the corresponding ASCII integer. So '\n' is the same as 10 as an integer.It all depends on the interpretation you give to the bytes.
在 C 表达式中,
char
被提升为int
。如果你仔细想想,这几乎可以解释每个问题。资料来源:《C 编程语言》,作者 Brian W.Kernighan 和 Dennis M.Ritchie
如果您想学习 C,则必须阅读。
另请参阅 这个堆栈溢出页面,比我更有经验的人可以比我更好地解释它。
In C
char
gets promoted toint
in expressions. That pretty much explains every question, if you think about it.Source: The C Programming Language by Brian W.Kernighan and Dennis M.Ritchie
A must read if you want to learn C.
Also see this stack overflow page, where people much more experienced then me can explain it much better then I ever can.
在 C 中,诸如
'\n'
或'a'
之类的字符常量表达式具有int
类型(因此sizeof '\n' == sizeof (int)
),而在 C++ 中,它们的类型为char
。语句
printf("%d", '\0');
应该简单地打印 0;表达式'\0'
的类型为int
,值为0。语句
printf("%d", ch);
应打印ch
中值的整数编码(对于 ASCII,'a'
== 97)。In C, character constant expressions such as
'\n'
or'a'
have typeint
(thussizeof '\n' == sizeof (int)
), whereas in C++ they have typechar
.The statement
printf("%d", '\0');
should simply print 0; the type of the expression'\0'
isint
, and its value is 0.The statement
printf("%d", ch);
should print the integer encoding for the value inch
(for ASCII,'a'
== 97).是的,除非你幸运,否则它会打印垃圾。
非常重要。
printf/sprintf/fprintf 参数的类型必须与关联的格式类型 char 匹配。
如果类型不匹配并且编译成功,则结果非常不确定。
许多较新的编译器都知道 printf 并在类型不匹配时发出警告。如果您收到这些警告,请修复它们。
如果要转换变量函数参数的类型,则必须提供强制转换(即显式转换),因为编译器无法确定需要执行转换(因为它可以使用带有类型化参数的函数原型) 。
在此示例中,printf 被告知堆栈上有一个“int”。强制转换确保无论 sizeof 返回什么(通常是某种长整数), printf 都会得到一个 int 。
Yes, it prints GARBAGE unless you are lucky.
VERY IMPORTANT.
The type of the printf/sprintf/fprintf argument MUST match the associated format type char.
If the types don't match and it compiles, the results are very undefined.
Many newer compilers know about printf and issue warnings if the types do not match. If you get these warnings, FIX them.
If you want to convert types for arguments for variable functions, you must supply the cast (ie, explicit conversion) because the compiler can't figure out that a conversion needs to be performed (as it can with a function prototype with typed arguments).
In this example, printf is being TOLD that there is an "int" on the stack. The cast makes sure that whatever thing sizeof returns (some sort of long integer, usually), printf will get an int.