How to printf “unsigned long” in C?

2019-01-07 01:58发布

I can never understand how to print unsigned long datatype in C.

Suppose unsigned_foo is an unsigned long, then I try:

  • 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 -123123123 number instead of unsigned long that I have.

8条回答
倾城 Initia
2楼-- · 2019-01-07 02:14
int main()
{
    unsigned long long d;
    scanf("%llu",&d);
    printf("%llu",d);
    getch();
}

This will be helpful . . .

查看更多
祖国的老花朵
3楼-- · 2019-01-07 02:15

For int %d

For long int %ld

For long long int %lld

For unsigned long long int %llu

查看更多
放我归山
4楼-- · 2019-01-07 02:16

The correct specifier for unsigned long is %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.

查看更多
forever°为你锁心
5楼-- · 2019-01-07 02:21
  • %lu for unsigned long
  • %llu for unsigned long long
查看更多
别忘想泡老子
6楼-- · 2019-01-07 02:21

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

查看更多
▲ chillily
7楼-- · 2019-01-07 02:29

%lu is 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?

查看更多
登录 后发表回答