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条回答
虎瘦雄心在
2楼-- · 2019-01-07 02:31

Out of all the combinations you tried, %ld and %lu are the only ones which are valid printf format specifiers at all. %lu (long unsigned decimal), %lx or %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).

查看更多
Root(大扎)
3楼-- · 2019-01-07 02:38

The format is %lu.

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

查看更多
登录 后发表回答