C语言 如何在C中打印“无符号长”?

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

How to printf "unsigned long" in C?

cprintflong-integerunsignedformat-specifiers

提问by bodacydo

I can never understand how to print unsigned longdatatype in C.

我永远无法理解如何unsigned long在 C 中打印数据类型。

Suppose unsigned_foois an unsigned long, then I try:

假设unsigned_foo是一个unsigned long,然后我尝试:

  • printf("%lu\n", unsigned_foo)
  • printf("%du\n", unsigned_foo)
  • printf("%ud\n", unsigned_foo)
  • printf("%ll\n", unsigned_foo)
  • printf("%ld\n", unsigned_foo)
  • printf("%dl\n", unsigned_foo)
  • printf("%lu\n", unsigned_foo)
  • printf("%du\n", unsigned_foo)
  • printf("%ud\n", unsigned_foo)
  • printf("%ll\n", unsigned_foo)
  • printf("%ld\n", unsigned_foo)
  • printf("%dl\n", unsigned_foo)

And all of them print some kind of -123123123number instead of unsigned longthat I have.

他们都打印了某种-123123123数字而不是unsigned long我的数字。

回答by Thanatos

%luis the correct format for unsigned long. Sounds like there are other issues at play here, such as memory corruption or an uninitialized variable. Perhaps show us a larger picture?

%lu是正确的格式unsigned long。听起来这里还有其他问题,例如内存损坏或未初始化的变量。也许给我们看一张更大的图?

回答by NealCaffery

  • %lufor unsigned long
  • %llufor unsigned long long
  • %lu对于无符号长
  • %llu对于unsigned long long

回答by Linkon

For int%d

对于整数%d

For long int%ld

对于长整数%ld

For long long int%lld

对于long long int%lld

For unsigned long long int%llu

对于unsigned long long int%llu

回答by R.. GitHub STOP HELPING ICE

Out of all the combinations you tried, %ldand %luare the only ones which are valid printf format specifiers at all. %lu(long unsigned decimal), %lxor %lX(long hex with lowercase or uppercase letters), and %lo(long octal) are the only valid format specifiers for a variable of type unsigned long (of course you can add field width, precision, etc modifiers between the %and the l).

在您尝试过的所有组合中,%ld并且%lu是唯一完全有效的 printf 格式说明符。%lu(长无符号十进制),%lx%lX(带小写或大写字母的长十六进制)和%lo(长八进制)是 unsigned long 类型变量的唯一有效格式说明符(当然,您可以在%l)。

回答by Praveen S

The format is %lu.

格式为%lu.

Please check about the various other datatypes and their usage in printf here

请在此处查看printf 中的各种其他数据类型及其用法

回答by Sanjith Bravo Dastan

int main()
{
    unsigned long long d;
    scanf("%llu",&d);
    printf("%llu",d);
    getch();
}

This will be helpful . . .

这会很有帮助。. .

回答by Kumar Alok

The correct specifier for unsigned long is %lu.

unsigned long 的正确说明符是%lu.

If you are not getting the exact value you are expecting then there may be some problems in your code.

如果您没有得到您期望的确切值,那么您的代码中可能存在一些问题。

Please copy your code here. Then maybe someone can tell you better what the problem is.

请在此处复制您的代码。那么也许有人可以更好地告诉你问题是什么。

回答by Joseleo

I had the same problem. Try "%ul", it works for me.

我有同样的问题。试试“%ul”,它对我有用。