Home » Questions » Computers [ Ask a new question ]

How do you format an unsigned long long int using printf?

How do you format an unsigned long long int using printf?

"#include <stdio.h>
int main() {
unsigned long long int num = 285212672; //FYI: fits in 29 bits
int normalInt = 5;
printf(""My number is %d bytes wide and its value is %ul. A normal number is %d.\n"", sizeof(num), num, normalInt);
return 0;
}

Output:

My number is 8 bytes wide and its value is 285212672l. A normal number is 0.

I assume this unexpected result is from printing the unsigned long long int. How do you printf() an unsigned long long int?"

Asked by: Guest | Views: 317
Total answers/comments: 4
Guest [Entry]

"Use the ll (el-el) long-long modifier with the u (unsigned) conversion. (Works in windows, GNU).

printf(""%llu"", 285212672);"
Guest [Entry]

"You may want to try using the inttypes.h library that gives you types such as
int32_t, int64_t, uint64_t etc.
You can then use its macros such as:

uint64_t x;
uint32_t y;

printf(""x: %""PRId64"", y: %""PRId32""\n"", x, y);

This is ""guaranteed"" to not give you the same trouble as long, unsigned long long etc, since you don't have to guess how many bits are in each data type."
Guest [Entry]

"%d--> for int

%u--> for unsigned int

%ld--> for long int or long

%lu--> for unsigned long int or long unsigned int or unsigned long

%lld--> for long long int or long long

%llu--> for unsigned long long int or unsigned long long"
Guest [Entry]

"For long long (or __int64) using MSVS, you should use %I64d:

__int64 a;
time_t b;
...
fprintf(outFile,""%I64d,%I64d\n"",a,b); //I is capital i"