Possible Duplicate:
how to printf uint64_t?
Why is it that on my 64-bit Mac (I am using Clang) the uint64_t
type is unsigned long long
while on 64-bit Ubuntu the uint64_t
type is unsigned long
?
This makes it very difficult for me to get my printf
calls to not give compiler warnings (or indeed even to work) under both environments.
I can try to use macros to try to choose the correct string (#define LU
either %llu
or %lu
, and in the process uglifying the printf
strings a bit) but on the Mac I've got a 64-bit word size (so _LP64
would be defined and UINTPTR_MAX != 0xffffffff
) and yet it still uses long long
for the 64 bit int types.
// printf macro switch (for the uint64_t's)
#if UINTPTR_MAX == 0xffffffff
// 32-bit
# define LU "%llu"
#else
// assume 64-bit
// special case for OS X because it is strange
// should actually check also for __MACH__
# ifdef __APPLE__
# define LU "%llu"
# else
# define LU "%lu"
# endif
#endif
"%f" is the (or at least one) correct format for a double. There is no format for a float , because if you attempt to pass a float to printf , it'll be promoted to double before printf receives it1.
%Lf format specifier for long double %lf and %Lf plays different role in printf. So, we should use %Lf format specifier for printing a long double value.
%lu. Unsigned int or unsigned long.
The %u format specifier is implemented for fetching values from the address of a variable having an unsigned decimal integer stored in the memory. It is used within the printf() function for printing the unsigned integer variable.
The macros are already defined for you in <cinttypes>
. Try
printf("%"PRIu64, x);
Or, even better, use C++ features like
std::cout << x;
which will select the proper << operator for your variable type.
The answer is to promote via static cast:
some_type i = 5;
printf("our value is: %llu", (unsigned long long)i);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With