函数 printf() 的问题
这是我的程序:
#include <stdio.h>
int main()
{
int a=0x09;
int b=0x10;
unsigned long long c=0x123456;
printf("%x %llx\n",a,b,c);//in "%llx", l is lowercase of 'L', not digit 1
return 0;
}
输出是:
9 12345600000010
我想知道:
- 函数 printf() 是如何执行的?
- 如果参数的数量不等于格式的数量会发生什么?
请帮助我并以该程序为例进行解释。
Here is my program:
#include <stdio.h>
int main()
{
int a=0x09;
int b=0x10;
unsigned long long c=0x123456;
printf("%x %llx\n",a,b,c);//in "%llx", l is lowercase of 'L', not digit 1
return 0;
}
the output was:
9 12345600000010
I want to know:
- how function printf() is executed?
- what will happen if the number of arguments isn't equal to that of formats?
please help me and use this program as an example to make an explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是你的类型不匹配。这是未定义的行为。
您的第二个参数
b
与格式的类型不匹配。所以发生的情况是printf()
正在读取包含 b 的 4 个字节(printf
需要一个 8 字节的操作数,但是b
是仅 4 个字节)。因此你得到的是垃圾。第三个参数根本不会打印,因为您的printf()
只有 2 个格式代码。由于参数通常在内存中连续(且相邻)传递,因此
printf()
正在读取的 4 个额外字节实际上是c
的低 4 个字节。所以最终,打印的第二个数字等于 b + ((c & 0xffffffff) << 32)。
但我想重申:此行为未定义。只是当今大多数系统的行为都是这样的。
The problem is that your types don't match. This is undefined behavior.
Your second argument
b
does not match the type of the format. So what's happening is thatprintf()
is reading past the 4 bytes holding b (printf
is expecting an 8-byte operand, butb
is only 4 bytes). Therefore you're getting junk. The 3rd argument isn't printed at all since yourprintf()
only has 2 format codes.Since the arguments are usually passed consecutively (and adjacent) in memory, the 4 extra bytes that
printf()
is reading are actually the lower 4 bytes ofc
.So in the end, the second number that's being printed is equal to
b + ((c & 0xffffffff) << 32)
.But I want to reiterate: this behavior is undefined. It's just that most systems today behave like this.
如果您传递给
printf
的参数与格式规范不匹配,那么您将得到未定义的行为。这意味着任何事情都可能发生,并且您无法推断在特定系统上碰巧看到的结果。在您的情况下,
%llx
需要类型为unsigned long long
的参数,但您提供了int
。仅此一点就会导致未定义的行为。向 printf 传递的参数多于格式规范并不是错误,多余的参数会被计算但会被忽略。
If the arguments that you pass to
printf
don't match the format specification then you get undefined behavior. This means that anything can happen and you cannot reason about the results that you happen to see on your specific system.In your case,
%llx
requires and argument of typeunsigned long long
but you supplied anint
. This alone causes undefined behaviour.It is not an error to pass more arguments to
printf
than there are format specificiers, the excess arguments are evaluated but ignored.printf()
增加一个指针,根据格式一次读取一个参数。如果格式化参数的数量大于参数的数量,则 printf() 将从未知的内存位置输出数据。但如果参数的数量大于格式化参数的数量,那么也没有什么坏处。例如,如果格式化参数和参数的数量不匹配,gcc 会警告您。printf()
increases a pointer to read an argument at a time according to the format. If the number of formatting arguments is larger than the number of parameters, then printf() will output data from unknown memory locations. But if the number of parameters is larger than the number of formatting arguments, then no harm was done. E.g. gcc will warn you if the number of formatting arguments and parameters don't match.