您的位置:首页 > 编程语言 > C语言/C++

C/C++的64位整型 不同编译器间的比较(附:各类型数据的范围)

2015-12-10 20:32 453 查看
        在C/C++中,64为整型一直是一种没有确定规范的数据类型。现今主流的编译器中,对64为整型的支持也是标准不一,形态各异。一般来说,64位整型的定义方式有long long和__int64两种(VC还支持_int64),而输出到标准输出方式有printf(“%lld”,a),printf(“%I64d”,a)和cout
<< a三种方式。
       本文讨论的是五种常用的C/C++编译器对64位整型的支持,这五种编译器分别是gcc(mingw32),g++(mingw32),gcc(linux i386),g++(linux i386),Microsoft Visual C++ 6.0。可惜的是,没有一种定义和输出方式组合,同时兼容这五种编译器。为彻底弄清不同编译器对64位整型,我写了程序对它们进行了评测,结果如下表。

变量定义输出方式gcc(mingw32)g++(mingw32)gcc(linux i386)g++(linux i386)MicrosoftVisual C++ 6.0
long long“%lld”错误错误正确正确无法编译
long long“%I64d”正确正确错误错误无法编译
__int64“lld”错误错误无法编译无法编译错误
__int64“%I64d”正确正确无法编译无法编译正确
long longcout非C++正确非C++正确无法编译
__int64cout非C++正确非C++无法编译无法编译
long longprintint64()正确正确正确正确无法编译
上表中,正确指编译通过,运行完全正确;错误指编译虽然通过,但运行结果有误;无法编译指编译器根本不能编译完成。观察上表,我们可以发现以下几点:
long long定义方式可以用于gcc/g++,不受平台限制,但不能用于VC6.0。
__int64是Win32平台编译器64位长整型的定义方式,不能用于Linux。
“%lld”用于Linux i386平台编译器,”%I64d”用于Win32平台编译器。
cout只能用于C++编译,在VC6.0中,cout不支持64位长整型。
表中最后一行输出方式中的printint64()是我自己写的一个函数,可以看出,它的兼容性要好于其他所有的输出方式,它是一段这样的代码:
void print_int64(long long a){
if(a <= 100000000)
printf("%d/n",a);
else{
printf("%d",a/100000000);
printf("%08d/n",a%100000000);
}
}
这种写法的本质是把较大的64位整型拆分为两个32位整型,然后依次输出,低位的部分要补0。看似很笨的写法,效果如何?我把它
和cout输出方式做了比较,因为它和cout都是C++支持跨平台的。首先printint64()和cout(不清空缓冲区)的运行结果是完全相同的,
不会出现错误。我的试验是分别用两者输出1000000个随机数,实际结果是,printint64()在1.5s内跑完了程序,而cout需要2s。cout
要稍慢一些,所以在输出大量数据时,要尽量避免使用。

64位整数全解(增补板) 

 

64位整形引起的混乱主要在两方面,一是数据类型的声明,二是输入输出。

首先是如果我们在自己机器上写程序的话,情况分类如下:

(1) 在win下的VC6.0里面,声明数据类型的时候应该写作

__int64 a;

输入输出的时候用 %I64d

scanf(”%I64d”,&a);

printf(”%I64d”,a);

(2) 在linux下的gcc/g++里面,数据类型声明写作

long long a;

输入输出时候用 %lld

(3) 在win下的其它IDE里面[包括高版本Visual Studio],数据类型声明用上面两种均可

输入输出用 %I64d

================== 以下可无视 =========================

以下是对这种混乱情况的解释,如无兴趣可以跳过

首先要说的是,和Java等语言不同,C/C++本身并没有规定各数据类型的位数,只是限定了一个大小关系,也就是规定从所占的bit数来说,short <= int <= long <= long long。至于具体哪种类型占用多少位,是由你所用的开发平台的编译器决定的。在现在的PC上一个通常的标准是,int和long同为32位,long long为64位。但是如果换到其它平台(如ARM)上,这个数字可能会有不同,类型所占的大小可以用sizeof()运算符查看。

long long是C99标准中新引进的数据类型,在古老的VC6.0中并没有这个类型,所以在VC6.0中用”long long”会发生编译错误。为了表示64位整数,VC6里采用的是微软自己搞出来的一个数据类型,叫做__int64,所以如果你是在VC6.0下编译的话,应该用__int64定义64位整型。新版的Visual Studio已经支持long long了。GCC是支持long long的,我们在win系统中使用的其它IDE如Dev-Cpp, Code::Blocks等等大多是采用的MinGW编译环境,它是与GCC兼容的,所以也支持long
long(另外为了与MS兼容,也支持__int64)。如果是在纯的linux下,就只能使用long long了。

关于使用printf的输入输出,这里就有一个更囧的情况。实际上只要记住,主要的区分在于操作系统:如果在win系统下,那么无论什么编译器,一律用%I64d;如果在linux系统,一律用%lld。这是因为MS提供的msvcrt.dll库里使用的就是%I64d的方式,尽管Dev-Cpp等在语法上支持标准,但也不得不使用MS提供的dll库来完成IO,所以就造成了这种情况。

==================== 无视至此 ===========================

那么对ACMer来说,最为关心的就是在各个OJ上交题应分别使用哪种方式了。其实方式只有有限的几种:

如果服务器是linux系统,那么定义用long long,IO用%lld

如果服务器是win系统,那么声明要针对编译器而定:

+ 如果用MS系列编译器,声明用__int64 [现在新版的Visual Studio也支持long long了]

+ 如果用MinGW环境,声明用long long

+ 无论什么编译器,IO一律%I64d

下面把各大OJ情况列表如下:

1. TOJ : Linux系统

2. ZOJ : Linux系统

3. POJ : Win系统,语言如选择C/C++,则用MS编译器[支持两种声明],如选择GCC/G++,则为MinGW

4. UVa : Linux系统

5. Ural: Win系统,MS编译器[支持两种声明]

6. SPOJ: Linux系统

7. SGU : Win系统,MS编译器[支持两种声明]

如果有不太清楚的情况可以先看看各OJ上的FAQ,通常会有说明。

另外,为了避免混乱,当数据量不大时,用cin, cout进行输入输出也是一种选择。
附:各类型数据的范围:
#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main(){
cout << "type: \t\t" << "************size**************"<< endl;
cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t" << "所占字节数:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits<char>::max)();
cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
cout << "\t最大值:" << (numeric_limits<signed char>::max)();
cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t" << "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t" << "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "long long: \t" << "所占字节数:" << sizeof(long long);
cout << "\t最大值:" << (numeric_limits<long long>::max)();
cout << "\t最小值:" << (numeric_limits<long long>::min)() << endl;
cout << "__int64: \t" << "所占字节数:" << sizeof(__int64);
cout << "\t最大值:" << (numeric_limits<__int64>::max)();
cout << "\t最小值:" << (numeric_limits<__int64>::min)() << endl;
cout << "double: \t" << "所占字节数:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max)();
cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t" << "所占字节数:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits<long double>::max)();
cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t" << "所占字节数:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max)();
cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
cout << "\t最大值:" << (numeric_limits<size_t>::max)();
cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
cout << "type: \t\t" << "************size**************"<< endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: