int64_t的定义

问题描述 投票:67回答:5

我是C / C ++的新手,所以我对基本类型有几个问题:

a)您能给我解释一下int64_tlonglong int)之间的区别吗?以我的理解,两者都是64位整数。有什么理由选择一个吗?

b)我试图在网络上查找int64_t的定义,但没有成功。我需要咨询有关此类问题的权威信息吗?

c)对于使用int64_t进行编译的代码,我当前包含<iostream>,这对我来说没有多大意义。是否还有其他提供int64_t声明的包含项?

c++ c integer long-integer
5个回答
84
投票
a)您能给我解释一下int64_tlonglong int)之间的区别吗?以我的理解,两者都是64位整数。有什么理由选择一个吗?
前者是具有

exactly 64位的有符号整数类型。后者是

至少 32位的有符号整数类型。

b)我试图在网络上查找int64_t的定义,但没有成功。我需要咨询有关此类问题的权威信息吗?
[http://cppreference.com”在这里涵盖:http://en.cppreference.com/w/cpp/types/integer。但是,权威来源是C++ standard(可以在§18.4整数类型[cstdint]中找到此特定位)。

c)对于使用int64_t进行编译的代码,我包括<iostream>,这对我来说没有多大意义。是否还有其他提供int64_t声明的包含项?

<cstdint><cinttypes>(在名称空间std下),或在<stdint.h><inttypes.h>(在全局名称空间中)中声明。

10
投票
精确地 64位宽,对于long至少32位没有这样的保证,因此它可以更多。

§7.18.1.3精确宽度整数类型1 typedef名称intN_t指定宽度为N,无填充位的有符号整数类型,并且二的补码表示。因此,int8_t表示一个有符号整数类型,宽度恰好为8位。


4
投票

2
投票

0
投票
Linux

第一long longlong int是不同的类型但是sizeof(long long) == sizeof(long int) == sizeof(int64_t)

Gcc

首先尝试找到编译器在哪里以及如何定义int64_t和uint64_t

grepc -rn "typedef.*INT64_TYPE" /lib/gcc /lib/gcc/x86_64-linux-gnu/9/include/stdint-gcc.h:43:typedef __INT64_TYPE__ int64_t; /lib/gcc/x86_64-linux-gnu/9/include/stdint-gcc.h:55:typedef __UINT64_TYPE__ uint64_t;

所以我们需要找到此编译器宏定义

gcc -dM -E -x c /dev/null | grep __INT64                 
#define __INT64_C(c) c ## L
#define __INT64_MAX__ 0x7fffffffffffffffL
#define __INT64_TYPE__ long int

gcc -dM -E -x c++ /dev/null | grep __INT64
#define __INT64_C(c) c ## L
#define __INT64_MAX__ 0x7fffffffffffffffL
#define __INT64_TYPE__ long int

Clang

clang -dM -E -x c++ /dev/null | grep INT64_TYPE
#define __INT64_TYPE__ long int
#define __UINT64_TYPE__ long unsigned int

Clang,GNU编译器:-dM转储宏列表。-E将结果打印到标准输出而不是文件。-x c-x c++在使用不带文件扩展名的文件时选择编程语言,例如/dev/null
参考:https://web.archive.org/web/20190803041507/http://nadeausoftware.com/articles/2011/12/c_c_tip_how_list_compiler_predefined_macros

注:对于swig用户,在Linux x86_64上使用-DSWIGWORDSIZE64

MacOS

在Catalina 10.15 IIRC上

Clang

clang -dM -E -x c++ /dev/null | grep INT64_TYPE #define __INT64_TYPE__ long long int #define __UINT64_TYPE__ long long unsigned int

C语:-dM转储宏列表。-E将结果打印到标准输出而不是文件。-x c-x c++在使用不带文件扩展名的文件时选择编程语言,例如/dev/null
参考:https://web.archive.org/web/20190803041507/http://nadeausoftware.com/articles/2011/12/c_c_tip_how_list_compiler_predefined_macros

注:对于swig用户,在macOS x86_64上

不要使用-DSWIGWORDSIZE64

Visual Studio 2019第一sizeof(long int) == 4sizeof(long long) == 8

stdint.h中,我们有:

#if _VCRT_COMPILER_PREPROCESSOR typedef signed char int8_t; typedef short int16_t; typedef int int32_t; typedef long long int64_t; typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; typedef unsigned long long uint64_t;

注意:对于Swig用户,在Windows x86_64上

不要使用-DSWIGWORDSIZE64

请参阅:https://github.com/swig/swig/blob/3a329566f8ae6210a610012ecd60f6455229fe77/Lib/stdint.i#L20-L24

© www.soinside.com 2019 - 2024. All rights reserved.