long int

long int即long,给人的感觉好像是长整型,但实际上,它和int一样,只有32位。cppreference给出的定义是——

int - basic integer type. The keyword int may be omitted if any of the modifiers listed below are used. If no length modifiers are present, it’s guaranteed to have a width of at least 16 bits. However, on 32/64 bit systems it is almost exclusively guaranteed to have width of at least 32 bits.
long - target type will have width of at least 32 bits.

在实际的使用中,long与int几乎没有区别,比如——

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>

int main(){
long l = 10000000;
int i = 10000000;
printf("size of long:%d\n",sizeof(long));
printf("size of int:%d\n",sizeof(int));
printf("l = %d\n",l);
printf("i = %d\n",i);
return 0;
}
1234567891011

在这里插入图片描述
  既然long int与int相同,那么为什么还有long int这种尴尬的类型呢?
  原因是早期的C编译器定义了long int占用4个字节,int占用2个字节,long int是名副其实的长整型。在ANSI C的标准中,对长整型的定义也是long int应该至少和int一样长,而不是long int 一定要比int占用存储字节长。新版的C/C++标准兼容了早期的这一设定。

long long

long long则不同,long long是C++的64位整型的基本类型,“现任”长整型,从C99开始引入这个概念,在后续的标准中完善概念和定义,C++11官方正式标准如下——

long long - target type will have width of at least 64 bits.(since C++11)

long long占用8个字节,数据表示范围也从int的[ − 2 31 , 2 31 − 1 ] [-2^{31},2^{31}-1][−231,231−1],升级到[ − 2 63 , 2 63 − 1 ] [-2^{63},2^{63}-1][−263,263−1]。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>

int main(){
long l = 10000000;
int i = 10000000;
long long ll = 100000000000000;
printf("size of long:%d\n",sizeof(long));
printf("size of int:%d\n",sizeof(int));
printf("size of long long:%d\n",sizeof(long long));
printf("l = %d\n",l);
printf("i = %d\n",i);
printf("ll = %lld\n",ll);
return 0;
}
1234567891011121314

在这里插入图片描述
  long long是C++的正式标准,这也就意味着,该类型不会因为编译器或者开发平台的不同而有所差异,放之四海而皆准,而且与一些系统函数、类库都有很好的交互(如常见的printf、scanf、cin和cout等)。与之相反,出身MS的__int64就有点不受待见了,在不同的编译器上可能水土不服。

__int64

在早期的代码中,可能会遇到__int64这种类型,这应该算是“历史遗留”问题。早期的C/C++标准中并没有规定64位长整型的规范,因此不同的编译器对这一模糊概念有不同的定义,我们熟知的VC6.0采用了__int64这种类型来表示64位长整型——

1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<cmath>
using namespace std;
int main(){
__int64 i64 = -pow(2,63);
cout<<"__int64:"<<sizeof(__int64)<<endl;
cout<<"i64 = "<<i64<<endl;
return 0;
}
123456789

在这里插入图片描述
  与long long具有相同的效果,但是,__int64并不是正式规范,比如,如果你使用的是MinGW的编译器的话,打开__int64的定义会发现——
img
  VS 2017虽然把__int64作为了一种内置类型,但是也做了类似的处理,使得__int64与long long没有肉眼可见的差别。但如果你使用的开发环境比较早期,那么可能就要专门研究一下__int64了。比如说,古老的VC6.0并没有long long这种类型,而__int64的输入输出,又需要printf和%I64d的配合,而不是%lld或者直接用cout。

总结

C/C++与Java等语言不同,具有较高的自由度,由于某些历史原因,某些概念在不同的平台上有不同的解释,虽然现在新版本的C++标准和编译器都在做兼容性工作,你可以在__int64与long long之间无缝切换,但至少,我们要清楚两者的不同。

类型 存储字节 表示范围
int 4 -2147483648~2147483647
short int 2 -32768~+32767
long 4 -2147483648~2147483647(二十亿,约1 0 10 10^{10}1010)
long long 8 9223372036854775808~+9223372036854775807(九百亿亿,约1 0 19 10^{19}1019)
__int64 8 9223372036854775808~+9223372036854775807

参考链接

1.https://blog.csdn.net/qq_31736627/article/details/52912691
2.https://en.cppreference.com/w/cpp/language/types
3.https://www.cnblogs.com/ChenDinghao/p/6480937.html
4.https://blog.csdn.net/sk18192449347/article/details/55000087