How to printf "unsigned long" in C?
Solution 1
%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?
Solution 2
For int %d
For long int %ld
For long long int %lld
For unsigned long long int %llu
Solution 3
%lufor unsigned long%llufor unsigned long long
Solution 4
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).
Solution 5
int main()
{
unsigned long long d;
scanf("%llu",&d);
printf("%llu",d);
getch();
}
This will be helpful . . .
bodacydo
My name is Boda Cydo. I am from Africa but now live in Washington DC.
Updated on July 08, 2022Comments
-
bodacydo 6 monthsI can never understand how to print
unsigned longdatatype in C.Suppose
unsigned_foois anunsigned 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
-123123123number instead ofunsigned longthat I have.