6

Possible Duplicate:
how to printf uint64_t?

I want to print u_int64_t in C. I want to know the format specifier for this? I am a C user, I want to do a printf().

Community
  • 1
  • 1
Invictus
  • 2,653
  • 8
  • 31
  • 50
  • Exact duplicate of [how to printf uint64_t](http://stackoverflow.com/q/8132399/62576). – Ken White Jul 24 '12 at 20:07
  • 1
    I want it for u_int64_t. Are both the same? I mean uint64_t and u_int64_t..? – Invictus Jul 24 '12 at 20:09
  • This question isn't an exact duplicate of the uint64_t question. The `u_int8_t`, `u_int16_t`, `u_int32_t`, and `u_int64_t` types show up in the Linux header: `/usr/include/sys/types.h`, with the comment `But these were defined by ISO C without the first '_'`. – Brian Vandenberg Jul 31 '15 at 22:04

2 Answers2

10
#include <inttypes.h>
#include <stdio.h>

uint64_t t = 42;
printf("%" PRIu64 "\n", t);
ouah
  • 142,963
  • 15
  • 272
  • 331
  • it is u_int64_t. So, I got confused.? Do they mean the same.? – Invictus Jul 24 '12 at 20:07
  • C has not standard C type named `u_int64_t` but has an optional type named `uint64_t`. – ouah Jul 24 '12 at 20:09
  • I am working on linux, and I came across this u_int64_t and I got confused, So, if I print the way you say, would it be fine..? – Invictus Jul 24 '12 at 20:12
  • You have to check what type `u_int64_t` is an alias for but assuming the type is a 64-bit width type, the `PRIu64` macro is the way to go. – ouah Jul 24 '12 at 20:15
  • 1
    It works, wooo. What is the 'PRIu64' meaning which is in the 'printf'? – aasa Jul 25 '12 at 03:17
  • 1
    @aasa it's a macro that expands to a string literal with the desired conversion specifier, for example `"lu"` or `"llu"` depending on your system. – ouah Jul 25 '12 at 08:54
1

You can use the L modifier for 64-bit integers, eg:

u_in64_t number;
printf("%Lu", number);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • `L` is not a C standard conversion specifier – ouah Jul 24 '12 at 20:12
  • So, what is the final printf format? – Invictus Jul 24 '12 at 20:15
  • Standard C does not have its own 64-bit integer data type, but the compiler may support `long long ...`, which could be typedefed. `long long ...` uses the `ll` modifier instead. But some compilers also support the `L` modifier. – Remy Lebeau Jul 24 '12 at 20:22
  • 1
    Standard C does not guarantee a 64-bit integer data type, but if the platform has a 64-bit integer data type, then a C99 implementation is required to provide `uint64_t` (section 7.18.1.1). `uint_least64_t` and `uint_fast64_t` are required (sections 7.18.1.2 and 7.18.1.3), and there are corresponding `PRIu64`/`PRIuLEAST64`/`PRIuFAST64` macros. – jamesdlin Jul 24 '12 at 20:29