C语言 从指针打印地址时 %lx 和 %ld 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26223642/
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 11:26:26  来源:igfitidea点击:

What is the difference between %lx and %ld when printing an address from pointer?

c

提问by Ray

I have tried googling the first one for %lx, but I have no good results, BUT I have successfully searched up %ld which is just long int. Necessary for printing addresses I guess, but what is %lx for?

我试过在谷歌上搜索 %lx 的第一个,但我没有好的结果,但我已经成功搜索了 %ld,它只是 long int。我猜是打印地址所必需的,但是 %lx 有什么用?

This is where I am confused:

这是我感到困惑的地方:

int main()
{
    int value = 25;
    int *pointer = &value;
    printf("%ld\n", pointer);  // prints out the address of variable value( I hope)
    printf("0x%lx\n", pointer); // Completely confused here, is this perhaps address in hex?
}

Would be awesome if someone can clear this confusion I am having!

如果有人能清除我遇到的这种困惑,那就太棒了!

I have ran this code, and I have the results, but I am still not sure what the lx does..I have seriously tried googling this "%lx" in google, but no results explaining it.

我已经运行了这段代码,我得到了结果,但我仍然不确定 lx 是做什么的。

Edit: if I use 'p' to print address then have I been wrong in thinking %ld prints address? Confused.

编辑:如果我使用“p”来打印地址,那么我认为 %ld 打印地址是错误的吗?使困惑。

回答by Bill Lynch

They're both undefined behavior.

它们都是未定义的行为。

To print a pointer with printf, you should cast the pointer to void *and use "%p".

要使用 printf 打印指针,您应该将指针强制转换为void *并使用"%p".

That being said:

话虽如此:

We can talk about the difference between "%ld"and "%lx"when trying to print integers. %ldexpects a variable of type long int, and %lxexpects a variable of type long unsigned int.

我们可以讨论"%ld""%lx"尝试打印整数之间的区别。%ld需要一个类型的变量long int,并%lx需要一个类型的变量long unsigned int

More or less though, The difference between x, o, dand uare about how numbers are going to be printed.

或多或少xod和之间的区别在于u如何打印数字。

  • xprints an unsigned number in hexadecimal.
  • oprints an unsigned number in octal.
  • uprints an unsigned number in decimal.
  • dprints a signed number in decimal.
  • iprints a signed number in decimal.
  • x以十六进制打印一个无符号数。
  • o以八进制打印一个无符号数。
  • u以十进制打印一个无符号数。
  • d以十进制打印有符号数。
  • i以十进制打印有符号数。

We can then attach lto the format string for formats like %lxto specify that instead of an int, we're using a long int(That is, an unsigned long int, or long int).

然后,我们可以将l格式附加到格式字符串上,例如%lx指定int我们使用的是 a long int(即 anunsigned long intlong int)而不是 an 。

There is a table at cppreference that has additional information: http://en.cppreference.com/w/c/io/fprintf

cppreference 中有一个表,其中包含其他信息:http: //en.cppreference.com/w/c/io/fprintf

回答by Farouq Jouti

%pand %lxprints the address in hexadecimal while %ldprints it in decimal

%pand %lxprints the address in hexadecimal while %ldprints it in decimal