C ++标准规定int,longtypes的大小是什么?

我正在寻找关于基本C ++types的大小的详细信息。 我知道这取决于体系结构(16位,32位,64位)和编译器。

但是有没有C ++的标准?

我在32位体系结构上使用Visual Studio 2008。 这是我得到的:

char : 1 byte short : 2 bytes int : 4 bytes long : 4 bytes float : 4 bytes double: 8 bytes 

我试图在没有太多成功的情况下find可靠的信息,说明不同架构和编译器下的charshortintlongdoublefloat (以及其他types,我没有想到)的大小。

C ++标准没有以字节为单位指定整型的大小,但它指定了它们必须能够保持的最小范围。 您可以从所需的范围内推断出最小的位数。 您可以从中推断出最小字节数, CHAR_BITmacros的值定义了一个字节中的位数 (除8个最不明显的平台以外,不能less于8个)。

char另一个约束是它的大小总是1个字节,或者CHAR_BIT位(因此是名字)。

标准要求的最小范围(第22页)是:

和MSDN上的数据types范围:

  1. signed char :-127到127(注意,不是-128到127;这适用于1的补码和符号和大小的平台)
  2. unsigned char :0到255
  3. “plain” char :与signed charunsigned char相同的范围,由实现定义
  4. signed short :-32767至32767
  5. unsigned short :0到65535
  6. signed int :-32767至32767
  7. unsigned int :0到65535
  8. signed long :-2147483647至2147483647
  9. unsigned long整数:0到4294967295
  10. signed long long :-9223372036854775807到9223372036854775807
  11. unsigned long long :0到18446744073709551615

一个C ++(或C)实现可以定义一个types的大小以字节sizeof(type)为任意值,只要

  1. expression式sizeof(type) * CHAR_BIT计算结果高到足以包含所需范围的位数
  2. types的sorting仍然有效(例如sizeof(int) <= sizeof(long) )。

实际的特定实现范围可以在C中的<limits.h>头文件中find,或者在C ++中find<climits> (甚至更好,在<limits>头文件<climits>模板化的std::numeric_limits )。

例如,这是你将如何findint最大范围:

C:

 #include <limits.h> const int min_int = INT_MIN; const int max_int = INT_MAX; 

C ++

 #include <limits> const int min_int = std::numeric_limits<int>::min(); const int max_int = std::numeric_limits<int>::max(); 

对于32位系统,“事实上”的标准是ILP32–即intlong和pointer都是32位的数量。

对于64位系统,主要的Unix“事实上”标准是LP64- long ,指针是64位(但是int是32位)。 Windows 64位标准是LLP64 – long long ,指针是64位(但longint都是32位)。

曾经有一些Unix系统使用ILP64组织。

C标准(ISO / IEC 9899:1999)没有规定这些事实上的标准,但都允许这样做。

而且,根据定义, sizeof(char)1 ,尽pipe在Perlconfiguration脚本中进行了testing。

请注意,有些机器(Crays)的CHAR_BIT比8大得多。这意味着IIRC, sizeof(int)也是1,因为charint都是32位的。

实际上没有这样的事情。 您可以期望std::size_t始终表示当前体系结构上的无符号本地整数大小。 即16位,32位或64位。

但是就所有其他的内置types而言,它依赖于编译器。 下面是从最新C ++标准的当前工作草案中摘录的两段摘录:

有五种标准的有符号整数types:signed char,short int,int,long int和long long int。 在这个列表中,每个types至less提供了与列表中的前一个相同的存储空间。

对于每个标准的有符号整数types,都存在一个相应的(但不同的)标准无符号整数types:无符号整型,无符号短整型,无符号整型,无符号长整型和无符号长整型,每一个都占用相同数量存储并具有相同的alignment要求。

如果你想静态(编译时)可以断言这些基本types的大小。 如果假设的大小发生变化,它会提醒人们考虑移植你的代码。

有标准。

C90标准要求

 sizeof(short) <= sizeof(int) <= sizeof(long) 

C99标准要求

 sizeof(short) <= sizeof(int) <= sizeof(long) < sizeof(long long) 

这是C99的规格 。 不同整数types的细节大小。

这里是Windows平台的inttypes的大小(位):

 Type C99 Minimum Windows 32bit char 8 8 short 16 16 int 16 32 long 32 32 long long 64 64 

如果您关心的是可移植性,或者您希望types的名称反映了大小,您可以查看头文件<inttypes.h> ,其中以下macros可用:

 int8_t int16_t int32_t int64_t 

int8_t保证是8位,而int16_t保证是16位等。

如果您需要固定大小的types,请使用stdint.h中定义的typesuint32_t(无符号整数32位)。 它们在C99中指定。

更新:C ++ 11将TR1中的types正式纳入标准:

  • long long int
  • 无符号long long int

以及来自<cstdint>的“大小”types

  • 中int8_t
  • int16_t
  • int32_t
  • 的int64_t
  • (和未签名的同行)。

另外你会得到:

  • int_least8_t
  • int_least16_t
  • int_least32_t
  • int_least64_t
  • 加上未签名的同行。

这些types表示具有至less指定位数的最小整数types。 同样也有至less指定位数的“最快”整数types:

  • int_fast8_t
  • int_fast16_t
  • int_fast32_t
  • int_fast64_t
  • 加上未签名的版本。

如果有的话,“快”的意思就是实施。 它不一定是所有目的最快的。

C ++标准就是这样说的:

3.9.1,§2:

有五个有符号整数types:“signed char”,“short int”,“int”,“long int”和“long long int”。 在这个列表中,每个types至less提供了与列表中的前一个相同的存储空间。 普通整数具有执行环境的体系结构所build议的自然大小(44)。 提供其他有符号整数types以满足特殊需求。

(44),其大小足以包含在头部<climits>定义的INT_MIN和INT_MAX范围内的任何值

结论:这取决于你正在工作的架构。 任何其他的假设是错误的。

不,没有尺寸的标准。 标准只要求:

 sizeof(short int) <= sizeof(int) <= sizeof(long int) 

如果你需要一个固定大小的variables,你可以做的最好的事情就是像这样使用macros:

 #ifdef SYSTEM_X #define WORD int #else #define WORD long int #endif 

然后你可以使用WORD来定义你的variables。 这不是我喜欢这个,而是最便携的方式。

我们可以定义一个types的同义词,所以我们可以创build自己的“标准”。

在sizeof(int)== 4的机器上,我们可以定义:

 typedef int int32; int32 i; int32 j; ... 

所以当我们将代码传输到其他机器,其中long int的大小实际上是4时,我们可以重新定义int的单个出现。

 typedef long int int32; int32 i; int32 j; ... 

对于浮点数有一个标准(IEEE754) :浮点数是32位,双精度是64.这是一个硬件标准,而不是一个C ++标准,所以编译器理论上可以定义float和double到其他大小,但是在实践中,从来没有见过使用任何不同的架构。

有一个标准,它是在各种标准文件(ISO,ANSI和什么)中指定的。

维基百科有一个很好的页面来解释各种types和它们可以存储的最大值: 整数在计算机科学。

但是,即使使用标准的C ++编译器,也可以使用以下代码片段相对轻松地find它们:

 #include <iostream> #include <limits> int main() { // Change the template parameter to the various different types. std::cout << std::numeric_limits<int>::max() << std::endl; } 

std :: numeric_limits的文档可以在Roguewavefind。 它包含了大量的其他命令,你可以调用它来找出各种限制。 这可以用于任何传递大小的任意types,例如std :: streamsize。

约翰的答案包含最好的描述,因为那些保证。 不pipe你在哪个平台上,还有另外一个好的页面可以详细说明每个types必须包含多less个比特: inttypes ,它们是在标准中定义的。

我希望这有帮助!

1)文章“ 被遗忘的64位程序开发问题 ”中的表N1

2)“ 数据模型 ”

您可以使用:

 cout << "size of datatype = " << sizeof(datatype) << endl; 

datatype = intlong int等等。你将能够看到你input的数据types的大小。

当涉及到不同体系结构和不同编译器的内置types时,只需在您的架构上使用编译器运行以下代码,以查看它输出的内容。 下面显示我的Ubuntu 13.04 (Raring Ringtail)64位g ++ 4.7.3的输出。 另外请注意下面回答什么,这是为什么输出是这样命令:

“有五个标准的有符号整数types:signed char,short int,int,long int和long long int。在这个列表中,每个types提供的存储量至less与列表中的前面存储的数量相同。

 #include <iostream> int main ( int argc, char * argv[] ) { std::cout<< "size of char: " << sizeof (char) << std::endl; std::cout<< "size of short: " << sizeof (short) << std::endl; std::cout<< "size of int: " << sizeof (int) << std::endl; std::cout<< "size of long: " << sizeof (long) << std::endl; std::cout<< "size of long long: " << sizeof (long long) << std::endl; std::cout<< "size of float: " << sizeof (float) << std::endl; std::cout<< "size of double: " << sizeof (double) << std::endl; std::cout<< "size of pointer: " << sizeof (int *) << std::endl; } size of char: 1 size of short: 2 size of int: 4 size of long: 8 size of long long: 8 size of float: 4 size of double: 8 size of pointer: 8 

如上所述,大小应该反映当前的架构。 如果你想看看你当前的编译器是如何处理的,你可以在limits.h一个高峰。

如果您对纯粹的C ++解决scheme感兴趣,那么我使用模板和C ++标准代码来根据它们的位大小在编译时定义types。 这使得解决scheme可以在编译器之间移植。

后面的想法非常简单:创build一个包含char,int,short,long,long long(有符号和无符号版本)types的列表,并扫描列表并使用numeric_limits模板select给定大小的types。

包括这个头你有8种typesstdtype :: int8,stdtype :: int16,stdtype :: int32,stdtype :: int64,stdtype :: uint8,stdtype :: uint16,stdtype :: uint32,stdtype :: uint64。

如果某些types不能被表示,它将被评估为stdtype :: null_type也在该头中声明。

下面的代码没有保证,请双重检查。
我对编程有兴趣,随时可以编辑和修改本代码。
用DevC ++testing(所以3.5版本的gcc版本)

 #include <limits> namespace stdtype { using namespace std; /* * THIS IS THE CLASS USED TO SEMANTICALLY SPECIFY A NULL TYPE. * YOU CAN USE WHATEVER YOU WANT AND EVEN DRIVE A COMPILE ERROR IF IT IS * DECLARED/USED. * * PLEASE NOTE that C++ std define sizeof of an empty class to be 1. */ class null_type{}; /* * Template for creating lists of types * * T is type to hold * S is the next type_list<T,S> type * * Example: * Creating a list with type int and char: * typedef type_list<int, type_list<char> > test; * test::value //int * test::next::value //char */ template <typename T, typename S> struct type_list { typedef T value; typedef S next; }; /* * Declaration of template struct for selecting a type from the list */ template <typename list, int b, int ctl> struct select_type; /* * Find a type with specified "b" bit in list "list" * * */ template <typename list, int b> struct find_type { private: //Handy name for the type at the head of the list typedef typename list::value cur_type; //Number of bits of the type at the head //CHANGE THIS (compile time) exp TO USE ANOTHER TYPE LEN COMPUTING enum {cur_type_bits = numeric_limits<cur_type>::digits}; public: //Select the type at the head if b == cur_type_bits else //select_type call find_type with list::next typedef typename select_type<list, b, cur_type_bits>::type type; }; /* * This is the specialization for empty list, return the null_type * OVVERRIDE this struct to ADD CUSTOM BEHAVIOR for the TYPE NOT FOUND case * (ie search for type with 17 bits on common archs) */ template <int b> struct find_type<null_type, b> { typedef null_type type; }; /* * Primary template for selecting the type at the head of the list if * it matches the requested bits (b == ctl) * * If b == ctl the partial specified templated is evaluated so here we have * b != ctl. We call find_type on the next element of the list */ template <typename list, int b, int ctl> struct select_type { typedef typename find_type<typename list::next, b>::type type; }; /* * This partial specified templated is used to select top type of a list * it is called by find_type with the list of value (consumed at each call) * the bits requested (b) and the current type (top type) length in bits * * We specialice the b == ctl case */ template <typename list, int b> struct select_type<list, b, b> { typedef typename list::value type; }; /* * These are the types list, to avoid possible ambiguity (some weird archs) * we kept signed and unsigned separated */ #define UNSIGNED_TYPES type_list<unsigned char, \ type_list<unsigned short, \ type_list<unsigned int, \ type_list<unsigned long, \ type_list<unsigned long long, null_type> > > > > #define SIGNED_TYPES type_list<signed char, \ type_list<signed short, \ type_list<signed int, \ type_list<signed long, \ type_list<signed long long, null_type> > > > > /* * These are acutally typedef used in programs. * * Nomenclature is [u]intN where u if present means unsigned, N is the * number of bits in the integer * * find_type is used simply by giving first a type_list then the number of * bits to search for. * * NB. Each type in the type list must had specified the template * numeric_limits as it is used to compute the type len in (binary) digit. */ typedef find_type<UNSIGNED_TYPES, 8>::type uint8; typedef find_type<UNSIGNED_TYPES, 16>::type uint16; typedef find_type<UNSIGNED_TYPES, 32>::type uint32; typedef find_type<UNSIGNED_TYPES, 64>::type uint64; typedef find_type<SIGNED_TYPES, 7>::type int8; typedef find_type<SIGNED_TYPES, 15>::type int16; typedef find_type<SIGNED_TYPES, 31>::type int32; typedef find_type<SIGNED_TYPES, 63>::type int64; } 

正如其他人所回答的那样,这些“标准”都将大部分细节视为“实现定义”,并且只声明types“char”处于“char_bis”范围内,而“char <= short <= int <= long < = long long“(float和double几乎与IEEE浮点标准一致,long double通常与double相同 – 但在更多实现上可能更大)。

部分原因没有非常具体和精确的价值是因为像C / C ++这样的语言被devise为可移植到大量的硬件平台 – 包括“char”字大小可能是4位的计算机系统或7位,或者甚至除了普通家庭计算机用户所接触的“8- / 16- / 32- / 64-比特”计算机之外的某个值。 (这里的字长是指系统正常工作的位数 – 同样,家庭计算机用户也不一定总是8位)。

如果你真的需要一个特定位数的对象(在一系列代表整数值的位的意义上),大多数编译器都有一些指定方法; 但是它通常不是可移植的,甚至在由ame公司制作的编译器之间也是不同的平台。 有些标准和惯例(尤其是limits.h等)已经足够普遍,大多数编译器都将支持确定特定范围的值的最佳拟合types,而不是使用的位数。 (也就是说,如果你知道你需要保持0到127之间的值,你可以确定你的编译器支持8位的“int8”types,它将足够大以保持所需的全部范围,但不是像“int7”types,它将与7位完全匹配。)

注意:许多Un * x源代码包使用“./configure”脚本来检测编译器/系统的function并输出一个合适的Makefile和config.h。 您可能会检查其中的一些脚本,看看它们是如何工作的,以及它们如何调查这些编译器/系统function,并且遵循它们的指导。

我注意到这里的所有其他答案几乎完全集中在整数types上,而提问者也询问了浮点数。

我不认为C ++标准需要它,但是现在最常见平台的编译器通常遵循IEEE754标准的浮点数。 这个标准指定了四种types的二进制浮点(以及一些BCD格式,我从来没有在C ++编译器中看到这种格式):

  • 半精度(binary16) – 11位有效数,指数范围-14至15
  • 单精度(二进制32) – 24位有效数,指数范围-126至127
  • 双精度(binary64) – 53位有效数,指数范围-1022至1023
  • 四倍精度(binary128) – 113位有效位数,指数范围-16382至16383

那么如何映射到C ++types呢? 一般来说, float使用单精度; 因此, sizeof(float) = 4 。 然后double使用双精度(我相信这是双精度的来源), long double精度可能是双精度或四精度(这是我的系统的四倍,但在32位系统可能是双倍)。 我不知道任何提供半精度浮点的编译器。

总之,这是通常的:

  • sizeof(float) = 4
  • sizeof(double) = 8
  • sizeof(long double) = 8或16
 unsigned char bits = sizeof(X) << 3; 

其中X是一个charintlong等等。会给你X大小。

来自Alex B C ++标准没有以字节为单位指定整型的大小,但它指定了它们必须能够保持的最小范围。 您可以从所需的范围内推断出最小的位数。 您可以从中推断出最小字节数,CHAR_BITmacros的值定义了一个字节中的位数(除8个最不明显的平台以外,不能less于8个)。

char的另一个约束是它的大小总是1个字节,或者CHAR_BIT位(因此是名字)。

标准要求的最小范围(第22页)是:

和MSDN上的数据types范围:

有符号字符:-127到127(注意,不是-128到127;这适用于1补码平台)无符号字符:0到255“纯”字符:-127到127或0到255(取决于默认的字符签名)短:-32767〜32767 unsigned short:0〜65535 signed int:-32767〜32767 unsigned int:0〜65535 signed long:-2147483647〜2147483647 unsigned long:0〜4294967295 signed long long:-9223372036854775807〜9223372036854775807 unsigned long long: 0到18446744073709551615 C ++(或C)实现可以将字节sizeof(type)的大小定义为任何值,只要

expression式sizeof(type)* CHAR_BIT的计算结果为足以包含所需范围的位数,并且types的sorting仍然有效(例如sizeof(int)<= sizeof(long))。 实际的特定于实现的范围可以在C头文件中find,也可以在C ++中find(甚至更好,头文件中的模板std :: numeric_limits)。

例如,这是你将如何findint的最大范围:

C:

 #include <limits.h> const int min_int = INT_MIN; const int max_int = INT_MAX; 

C ++:

 #include <limits> const int min_int = std::numeric_limits<int>::min(); const int max_int = std::numeric_limits<int>::max(); 

这是正确的,然而,你也说得对:char:1字节短:2字节int:4字节长:4字节float:4字节double:8字节

由于32位体系结构仍然是默认的,也是最常用的,并且自内存不足时的32位以前的date起,它们保持了这些标准大小,而向后兼容性和标准化保持不变。 即使是64位系统也倾向于使用这些系统并进行扩展/修改。 请参考这个更多的信息:

http://en.cppreference.com/w/cpp/language/types

正如你所提到的 – 很大程度上取决于编译器和平台。 为此,请检查ANSI标准, http://home.att.net/~jackklein/c/inttypes.html

这是Microsoft编译器的一个: 数据types范围

你可以使用库提供的variables,如OpenGL , Qt等

例如,Qt 提供 qint8(在Qt支持的所有平台上保证是8位),qint16,qint32,qint64,quint8,quint16,quint32,quint64等

在64位机器上:

 int: 4 long: 8 long long: 8 void*: 8 size_t: 8 

There are four types of integers based on size:

  • short integer: 2 byte
  • long integer: 4 byte
  • long long integer: 8 byte
  • integer: depends upon the compiler (16 bit, 32 bit, or 64 bit)