在 C 中使用 %u 和 %d 打印内存地址之间的区别?

发布于 2024-10-20 21:35:40 字数 231 浏览 1 评论 0原文

我正在读一本C语言的书。为了打印出变量的内存地址,有时本书使用:

printf("%u\n",&n);

有时,作者写道:

printf("%d\n",&n);

结果总是相同的,但我不明白两者之间的区别(我知道 %u 表示无符号)。

有人可以详细说明一下吗?

多谢。

I reading a C book. To print out a memory address of a variable, sometimes the book uses:

printf("%u\n",&n);

Sometimes, the author wrote:

printf("%d\n",&n);

The result is always the same, but I do not understand the differences between the two (I know %u for unsigned).

Can anyone elaborate on this, please?

Thanks a lot.

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

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

发布评论

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

评论(5

尴尬癌患者 2024-10-27 21:35:40

%u 将整数视为无符号整数,而 %d 将整数视为有符号整数。如果整数介于 0 和 INT_MAX 之间(在 32 位系统上为 231-1),则两种情况的输出相同。

仅当整数为负数(对于有符号输入)或介于 INT_MAX+1UINT_MAX 之间(例如,介于 231 和 2 之间)时,才会产生影响。 32-1)。在这种情况下,如果您使用 %d 说明符,您将得到一个负数,而如果您使用 %u,您将得到一个很大的正数。

地址只有作为无符号数字才有意义,因此没有任何理由将它们打印为有符号数字。此外,当它们被打印出来时,它们通常以十六进制打印(带有 %x 格式说明符),而不是十进制。

不过,您实际上应该只使用 %p 格式说明符来表示地址——它保证适用于所有有效的指针。如果您使用的是 32 位整数但 64 位指针的系统,如果您尝试使用 %d%u 或 < code>%x 没有 ll 长度修饰符,您将得到错误的结果以及稍后打印的任何其他结果(因为 printf 只读取 4指针参数的 8 个字节);如果您确实添加了 ll 长度修饰符,那么您将无法移植到 32 位系统。

底线:始终使用 %p 打印指针/地址:

printf("The address of n is: %p\n", &n);
// Output (32-bit system): "The address of n is: 0xbffff9ec"
// Output (64-bit system): "The address of n is: 0x7fff5fbff96c"

确切的输出格式是实现定义的(C99 §7.19.6.1/8),但它几乎总是打印为无符号十六进制数字,通常以 0x 开头。

%u treats the integer as unsigned, whereas %d treats the integer as signed. If the integer is between 0 an INT_MAX (which is 231-1 on 32-bit systems), then the output is identical for both cases.

It only makes a difference if the integer is negative (for signed inputs) or between INT_MAX+1 and UINT_MAX (e.g. between 231 and 232-1). In that case, if you use the %d specifier, you'll get a negative number, whereas if you use %u, you'll get a large positive number.

Addresses only make sense as unsigned numbers, so there's never any reason to print them out as signed numbers. Furthermore, when they are printed out, they're usually printed in hexadecimal (with the %x format specifier), not decimal.

You should really just use the %p format specifier for addresses, though—it's guaranteed to work for all valid pointers. If you're on a system with 32-bit integers but 64-bit pointers, if you attempt to print a pointer with any of %d, %u, or %x without the ll length modifier, you'll get the wrong result for that and anything else that gets printed later (because printf only read 4 of the 8 bytes of the pointer argument); if you do add the ll length modifier, then you won't be portable to 32-bit systems.

Bottom line: always use %p for printing out pointers/addresses:

printf("The address of n is: %p\n", &n);
// Output (32-bit system): "The address of n is: 0xbffff9ec"
// Output (64-bit system): "The address of n is: 0x7fff5fbff96c"

The exact output format is implementation-defined (C99 §7.19.6.1/8), but it will almost always be printed as an unsigned hexadecimal number, usually with a leading 0x.

陌生 2024-10-27 21:35:40

当最高有效位未设置时,%d%u 将打印相同的结果。然而,这根本不是可移植的代码,也不是好的风格。我希望你的书比这个例子中看起来更好。

%d and %u will print the same results when the most significant bit is not set. However, this isn't portable code at all, and is not good style. I hope your book is better than it seems from this example.

穿越时光隧道 2024-10-27 21:35:40

你尝试了什么价值?无符号与有符号的区别,正如您所说,您知道。那么它做了什么以及您期望什么?

正符号值看起来与无符号值相同,所以我可以假设您使用较小的值来测试吗?负值怎么办?

最后,如果您尝试打印变量的地址(正如您所看到的那样),请使用 %p 代替。

What value did you try? The difference unsigned vs. signed, just as you said you know. So what did it do and what did you expect?

Positive signed values look the same as unsigned so can I assume you used a smaller value to test? What about a negative value?

Finally, if you are trying to print the variable's address (as it appears you are), use %p instead.

§普罗旺斯的薰衣草 2024-10-27 21:35:40

所有地址都是无符号的 32 位或 64 位,具体取决于机器(无法写入负地址)。使用 %d 并不合适,但通常会起作用。建议使用%u或%ul。

All addresses are unsigned 32-bit or 64-bit depending on machine (can't write to a negative address). The use of %d isn't appropriate, but will usually work. It is recommended to use %u or %ul.

a√萤火虫的光℡ 2024-10-27 21:35:40

没有这样的区别,如果你刚刚开始学习指针,请不要感到困惑。
%u 表示未签名的,%d 表示签名的

There is no such difference ,just don't get confused if u have just started learning pointers.
%u is for unsigned ones.And %d for signed ones

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