函数 printf() 的问题

发布于 2024-12-05 07:34:15 字数 406 浏览 0 评论 0原文

这是我的程序:

#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

我想知道:

  1. 函数 printf() 是如何执行的?
  2. 如果参数的数量不等于格式的数量会发生什么?

请帮助我并以该程序为例进行解释。

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:

  1. how function printf() is executed?
  2. 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 技术交流群。

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

发布评论

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

评论(3

一抹微笑 2024-12-12 07:34:15

问题是你的类型不匹配。这是未定义的行为。

您的第二个参数 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 that printf() is reading past the 4 bytes holding b (printf is expecting an 8-byte operand, but b is only 4 bytes). Therefore you're getting junk. The 3rd argument isn't printed at all since your printf() 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 of c.

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.

耳根太软 2024-12-12 07:34:15

如果您传递给 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 type unsigned long long but you supplied an int. 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.

你是年少的欢喜 2024-12-12 07:34:15

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.

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