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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5208641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:58:39  来源:igfitidea点击:

difference between printing a memory address using %u and %d in C?

cunsigned

提问by ipkiss

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

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

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).

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

Can anyone elaborate on this, please?

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

Thanks a lot.

非常感谢。

回答by Adam Rosenfield

%utreats the integer as unsigned, whereas %dtreats 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.

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

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

如果整数是负数(对于有符号输入)或介于INT_MAX+1和之间UINT_MAX(例如介于 2 31和 2 32-1之间),它只会有所不同。在这种情况下,如果您使用说明%d符,您将得到一个负数,而如果您使用%u,您将得到一个很大的正数。

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 %xformat specifier), not decimal.

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

You should really just use the %pformat 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 %xwithout the lllength modifier, you'll get the wrong result for that and anything else that gets printed later (because printfonly read 4 of the 8 bytes of the pointer argument); if you do add the lllength modifier, then you won't be portable to 32-bit systems.

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

Bottom line: always use %pfor printing out pointers/addresses:

底线:始终%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"

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.

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

回答by Raph Levien

%dand %uwill 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.

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

回答by Jonathan Wood

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.

最后,如果您尝试打印变量的地址(如您所见),请改用 %p。

回答by steveo225

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.

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

回答by Faraz Malik

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

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