32/64bit 作業系統 printf 列印 int64_t / uint64_t 的方法

本篇將介紹在 C/C++ 程式裡如何 printf 列印出 int64_t 或 uint64_t 這個變數類型,

int64_t 在 Linux 64-bit 作業系統 printf 列印要用 %ld
int64_t 在 Linux 32-bit 作業系統 printf 列印要用 %lld
uint64_t 在 Linux 64-bit 作業系統 printf 列印要用 %lu
uint64_t 在 Linux 32-bit 作業系統 printf 列印要用 %llu
int64_t 在 Windows 64-bit/32-bit 作業系統 printf 列印要用 %lld
uint64_t 在 Windows 64-bit/32-bit 作業系統 printf 列印要用 %llu

int64_t 或 uint64_t 會根據 32/64-bit 作業系統不同而定義不同類型,
如果想寫一種 32/64bit 作業系統都可以通用 printf 的程式碼,請養成好習慣,使用 PRId64PRIu64!詳細請看範例示範。

需要引入的 header 標頭檔: <inttypes.h>
PRId64PRIu64 是定義在 inttypes.h 裡,在 linux 和 windows 下都一樣,所以需要 include <inttypes.h>
雖然 windows 有 %I64d%I64u,但以程式碼移植性通用性的角度來說,使用 PRId64PRIu64 較好。

int64_t 與 uint64_t 的範例示範

以下範例示範怎麼 printf int64_t 和 printf uint64_t。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main(int argc, char *argv[])
{
/* print int64_t */
int64_t int64 = 100;
printf("int64: %" PRId64 "\n", int64);
//printf("int64: %ld\n", int64); // Linux 64 位元架構 (x86_64)
//printf("int64: %lld\n", int64); // Linux 32 位元架構 (x86)

/* print uint64_t */
uint64_t uint64 = 100;
printf("uint64: %" PRIu64 "\n", uint64);
//printf("uint64: %lu\n", uint64); // Linux 64 位元架構 (x86_64)
//printf("uint64: %llu\n", uint64); // Linux 32 位元架構 (x86)
return 0;
}

int64_t 與 uint64_t 的標頭檔定義

int64_t 與 uint64_t 定義在 stdint.h,來看看 stdint.h 怎樣定義 int64_t 以及 uint64_t。

在 Linux 下 stdint.h 是如何定義 int64_t 與 uint64_t
int64_t 在 64bit (WORDSIZE == 64) 下,int64_t 定義成 long int (print %ld)
否則 int64_t 定義成 long long int (print %lld)。
uint64_t 在 64bit (
WORDSIZE == 64) 下,uint64_t 定義成 unsigned long int (print %lu)
否則 uint64_t 定義成 unsigned long long int (print %llu)。

/usr/include/stdint.hstdint.h.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ...
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
# if __WORDSIZE == 64
typedef long int int64_t;
# else
__extension__
typedef long long int int64_t;
# endif
// ...
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
#ifndef __uint32_t_defined
typedef unsigned int uint32_t;
# define __uint32_t_defined
#endif
#if __WORDSIZE == 64
typedef unsigned long int uint64_t;
#else
__extension__
typedef unsigned long long int uint64_t;
#endif

在 Windows 下 stdint.h 是如何定義 int64_t 與 uint64_t
int64_t 在 64bit 與 32bit 下,int64_t 都定義成 long long (print %lld)。
uint64_t 在 64bit 與 32bit 下,uint64_t 都定義成 unsigned long long (print %llu)。

stdint.h
1
2
3
4
5
6
// ...
typedef int int32_t;
typedef long long int64_t;
// ...
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;

32/64bit 架構與 int64_t/uint64_t 的對應表格

以為下 ShengYu 彙整的表格,對應關係大概就是這樣,以後要用就方便了。

64 位元架構 32 位元架構 通用印法
int64_t (Linux) long int (%ld) long long int (%lld) PRId64
uint64_t (Linux) unsigned long int (%lu) unsigned long long int (%llu) PRIu64
int64_t (Windows) long long (%lld) long long (%lld) PRId64
uint64_t (Windows) unsigned long long (%llu) unsigned long long (%llu) PRIu64

參考
[1] 32/64位平台printf uint64的方法
https://blog.csdn.net/turkeyzhou/article/details/9029421
[2] 使用變數型別的良好習慣
http://b8807053.pixnet.net/blog/post/164224857
[2] How to print a int64_t type in C
https://stackoverflow.com/questions/9225567/how-to-print-a-int64-t-type-in-c
[4] c++ - printf format for unsigned __int64 on Windows - Stack Overflow
https://stackoverflow.com/questions/18107426/printf-format-for-unsigned-int64-on-windows
[5] Standard Types | Microsoft Docs
https://docs.microsoft.com/en-us/cpp/c-runtime-library/standard-types?view=vs-2019
[6] 一個長整數各自表述 (Size of long integer may vary in 64-bit systems)
https://dada.tw/2008/04/18/85/

相關主題
printf 格式化輸出說明
printf 列印 size_t 的方法
C++ 計算程式執行時間