C++ int64_t 的定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13604137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Definition of int64_t
提问by clstaudt
I am new to C/C++, so I have a couple of questions about a basic type:
我是 C/C++ 的新手,所以我有几个关于基本类型的问题:
a) Can you explain to me the difference between int64_t
and long
(long int
)?
In my understanding, both are 64 bit integers. Is there any reason to choose one over the other?
a) 你能向我解释一下int64_t
和long
( long int
)之间的区别吗?在我的理解中,两者都是 64 位整数。有什么理由选择其中之一吗?
b) I tried to look up the definition of int64_t
on the web, without much success. Is there an authoritative source I need to consult for such questions?
b) 我试图int64_t
在网上查找 的定义,但没有取得多大成功。对于此类问题,我需要咨询权威来源吗?
c) For code using int64_t
to compile, I am currently including <iostream>
, which doesn't make much sense to me. Are there other includes that provide a declaration of int64_t
?
c) 对于int64_t
用于编译的代码,我目前包括<iostream>
,这对我来说没有多大意义。是否有其他包含提供声明int64_t
?
回答by R. Martinho Fernandes
a) Can you explain to me the difference between
int64_t
andlong
(long int
)? In my understanding, both are 64 bit integers. Is there any reason to choose one over the other?
a) 你能向我解释一下
int64_t
和long
(long int
)之间的区别吗?在我的理解中,两者都是 64 位整数。有什么理由选择其中之一吗?
The former is a signed integer type with exactly64 bits. The latter is a signed integer type with at least32 bits.
前者是一个有符号整数类型,正好是64 位。后者是至少32 位的有符号整数类型。
b) I tried to look up the definition of
int64_t
on the web, without much success. Is there an authoritative source I need to consult for such questions?
b) 我试图
int64_t
在网上查找 的定义,但没有取得多大成功。对于此类问题,我需要咨询权威来源吗?
http://cppreference.comcovers this here: http://en.cppreference.com/w/cpp/types/integer. The authoritative source, however, is the C++ standard(this particular bit can be found in §18.4 Integer types [cstdint]).
http://cppreference.com在这里介绍了这个:http: //en.cppreference.com/w/cpp/types/integer。然而,权威来源是C++ 标准(这个特殊的位可以在 §18.4 整数类型 [cstdint] 中找到)。
c) For code using
int64_t
to compile, I am including<iostream>
, which doesn't make much sense to me. Are there other includes that provide a declaration ofint64_t
?
c) 对于
int64_t
用于编译的代码,我包含了<iostream>
,这对我来说没有多大意义。是否有其他包含提供声明int64_t
?
It is declared in <cstdint>
or <cinttypes>
(under namespace std
), or in <stdint.h>
or <inttypes.h>
(in the global namespace).
它在<cstdint>
或<cinttypes>
(在命名空间下std
)或在<stdint.h>
或<inttypes.h>
(在全局命名空间中)声明。
回答by iabdalkader
int64_t
is guaranteed by the C99 standard to be exactly64 bits wide on platforms that implement it, there's no such guarantee for a long
which is at least 32 bits so it could be more.
int64_t
C99 标准保证在实现它的平台上正好是64 位宽,对于long
至少 32 位的a 没有这样的保证,所以它可能更多。
§7.18.1.3 Exact-width integer types 1 The typedef name intN_t designates a signed integer type with width N , no padding bits, and a two's complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits.
§7.18.1.3 精确宽度整数类型 1 typedef 名称 intN_t 指定一个有符号整数类型,宽度为 N ,没有填充位,和一个二进制补码表示。因此, int8_t 表示宽度正好为 8 位的有符号整数类型。
回答by Omkant
int64_t
is typedef
you can find that in <stdint.h>
in C
int64_t
是typedef
,你可以发现,在<stdint.h>
用C
回答by Kylotan
An int64_t should be 64 bits wide on any platform (hence the name), whereas a long can have different lengths on different platforms. In particular, sizeof(long) is often 4, ie. 32 bits.
int64_t 在任何平台上都应该是 64 位宽(因此得名),而 long 在不同平台上可以有不同的长度。特别是,sizeof(long) 通常是 4,即。32 位。
回答by Mizux
My 2 cents, from a current implementation Point of View and for SWIG users on k8 (x86_64) architecture.
我的 2 美分,来自当前的实现观点和 k8 (x86_64) 架构上的 SWIG 用户。
Linux
Linux
First long long
and long int
are different types
but sizeof(long long) == sizeof(long int) == sizeof(int64_t)
首先long long
和long int
是不同的类型但是sizeof(long long) == sizeof(long int) == sizeof(int64_t)
Gcc
海湾合作委员会
First try to find where and how the compiler define int64_t and uint64_t
首先尝试找到编译器定义 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;
So we need to find this compiler macro definition
所以我们需要找到这个编译器宏定义
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 compilers:-dM
dumps a list of macros.-E
prints results to stdout instead of a file.-x c
and -x c++
select the programming language when using a file without a filename extension, such as /dev/null
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
note: for swig user, on Linux x86_64 use -DSWIGWORDSIZE64
注意:对于 swig 用户,在 Linux x86_64 上使用 -DSWIGWORDSIZE64
MacOS
苹果系统
On Catalina 10.15 IIRC
在 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
Clang:-dM
dumps a list of macros.-E
prints results to stdout instead of a file.-x c
and -x c++
select the programming language when using a file without a filename extension, such as /dev/null
Clang:-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
note: for swig user, on macOS x86_64 don'tuse -DSWIGWORDSIZE64
注意:对于 swig 用户,在 macOS x86_64 上不要使用-DSWIGWORDSIZE64
Visual Studio 2019
视觉工作室 2019
First
sizeof(long int) == 4
and sizeof(long long) == 8
首先
sizeof(long int) == 4
和sizeof(long long) == 8
in stdint.h
we have:
在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;
note: for swig user, on windows x86_64 don'tuse -DSWIGWORDSIZE64
注意:对于 swig 用户,在 windows x86_64 上不要使用-DSWIGWORDSIZE64
SWIG Stuff
SWIG 的东西
First see https://github.com/swig/swig/blob/3a329566f8ae6210a610012ecd60f6455229fe77/Lib/stdint.i#L20-L24so you can control the typedef using SWIGWORDSIZE64
but...
首先查看 https://github.com/swig/swig/blob/3a329566f8ae6210a610012ecd60f6455229fe77/Lib/stdint.i#L20-L24这样您就可以使用SWIGWORDSIZE64
但...
now the bad: SWIG Javaand SWIG CSHARPdo not take it into account
现在不好:SWIG Java和SWIG CSHARP没有考虑到它
So you may want to use
所以你可能想要使用
#if defined(SWIGJAVA)
#if defined(SWIGWORDSIZE64)
%define PRIMITIVE_TYPEMAP(NEW_TYPE, TYPE)
%clear NEW_TYPE;
%clear NEW_TYPE *;
%clear NEW_TYPE &;
%clear const NEW_TYPE &;
%apply TYPE { NEW_TYPE };
%apply TYPE * { NEW_TYPE * };
%apply TYPE & { NEW_TYPE & };
%apply const TYPE & { const NEW_TYPE & };
%enddef // PRIMITIVE_TYPEMAP
PRIMITIVE_TYPEMAP(long int, long long);
PRIMITIVE_TYPEMAP(unsigned long int, long long);
#undef PRIMITIVE_TYPEMAP
#endif // defined(SWIGWORDSIZE64)
#endif // defined(SWIGJAVA)
and
和
#if defined(SWIGCSHARP)
#if defined(SWIGWORDSIZE64)
%define PRIMITIVE_TYPEMAP(NEW_TYPE, TYPE)
%clear NEW_TYPE;
%clear NEW_TYPE *;
%clear NEW_TYPE &;
%clear const NEW_TYPE &;
%apply TYPE { NEW_TYPE };
%apply TYPE * { NEW_TYPE * };
%apply TYPE & { NEW_TYPE & };
%apply const TYPE & { const NEW_TYPE & };
%enddef // PRIMITIVE_TYPEMAP
PRIMITIVE_TYPEMAP(long int, long long);
PRIMITIVE_TYPEMAP(unsigned long int, unsigned long long);
#undef PRIMITIVE_TYPEMAP
#endif // defined(SWIGWORDSIZE64)
#endif // defined(SWIGCSHARP)
So int64_t
aka long int
will be bind to Java/C# long
on Linux...
所以int64_t
akalong int
将绑定到long
Linux 上的Java/C# ...