445

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.

Tardis
  • 465
  • 2
  • 10
bodacydo
  • 75,521
  • 93
  • 229
  • 319

7 Answers7

650

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

Thanatos
  • 42,585
  • 14
  • 91
  • 146
  • 19
    Oops, `%lu` worked this time. Thanks. Something else must have happened before and it didn't work. – bodacydo Jul 09 '10 at 04:52
  • 1
    @bodacydo: If you've got a bug, it might appear at semi-random... make sure your variable has a valid value before you try printing it. – Thanatos Jul 09 '10 at 04:54
  • 1
    Even if the variable is uninitialized, there should be no way printf reaches a point where it could print a minus sign when the format specifier was `%lu`. Technically it's undefined behavior but in reality the variable has an unpredictable value that gets passed to printf which printf then interprets as unsigned. I'm guessing bodacydo's original problem was flow reaching an incorrect printf call instead of the one intended... – R.. GitHub STOP HELPING ICE Jul 09 '10 at 06:24
  • Very helpful, Thanatos. I tried %ul didn't work. How is ul different from lu? – Aquarius_Girl Feb 10 '12 at 09:36
  • 11
    @Anisha Kaul: %lu is a valid conversion specification, %ul is not. `%lu`, broken out is: `%` — starts a "conversion specification"; `l` — the length modifier, `l` means "[unsigned] long int"; `u` — the conversion specifier, `u` is for an `unsigned int` to be printed out as decimal. Because we gave the length modifier `l`, it then accepts an `unsigned long int`. The letters *must* be in that order: percent, length, conversion. (There are a few more options, such as width and precision, that you can add. *See the man page*, as it documents all this precisely!) – Thanatos Feb 14 '12 at 07:12
  • 1
    %ul will just print unsigned (with %u), and then the letter "l" verbatim. Just as "%uw" will print unsigned, followed by letter "w". % starts the convspec, u (or some other character, like d, s, c...) ends it. – Veky Jun 11 '13 at 03:27
54

For int %d

For long int %ld

For long long int %lld

For unsigned long long int %llu

Linkon
  • 1,058
  • 1
  • 12
  • 15
41
  • %lu for unsigned long
  • %llu for unsigned long long
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
NealCaffery
  • 522
  • 6
  • 14
25

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

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 2
    %ld will work fine till the value of std::numeric_limits::max()/2. Above that %ld will print wrong value(negative value). – Kaushik Acharya Aug 22 '13 at 09:28
  • 2
    @KaushikAcharya: No, above that it's UB. And it's arguably even UB before that, since `printf` is specified to require the exact correct argument types without the allowances that `va_arg` would have. – R.. GitHub STOP HELPING ICE Dec 02 '14 at 05:00
13

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.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
Kumar Alok
  • 2,512
  • 8
  • 26
  • 36
13

The format is %lu.

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

whoan
  • 8,143
  • 4
  • 39
  • 48
Praveen S
  • 10,355
  • 2
  • 43
  • 69
  • 2
    Various nonstandard behavior is documented there without marking it as nonstandard. POSIX is the easiest-to-access online standard for printf; extensions beyond ISO C are marked with the "CX" tag: http://www.opengroup.org/onlinepubs/9699919799/functions/fprintf.html – R.. GitHub STOP HELPING ICE Jul 09 '10 at 06:27
11
int main()
{
    unsigned long long d;
    scanf("%llu",&d);
    printf("%llu",d);
    getch();
}

This will be helpful . . .

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86