typedef可以把数据类型重定义,即给数据类型起别名。
typedef提高程序可移植性
#include<iostream>
#include<windows.h>
using namespace std;
// long类型32为系统上为32位,64位系统上为64位。
// 程序移植到32位系统上只需要改为long long即可
typedef long long int64;
int main() {
int64 num = 100000000000;
cout << num << " " << sizeof(int64) << endl;
system("pause");
return 0;
}
结果:
100000000000 8
请按任意键继续. . .
typedef和宏定义的区别
typedef和宏定义区别主要在指针
#include<iostream>
#include<windows.h>
using namespace std;
#define DSTR char*
typedef char* TSTR;
int main() {
DSTR s1, s2; // s1是char*,s2是char
TSTR s3, s4; // s3是char*,s4是char*
cout << sizeof(s1) << " " << sizeof(s2) << endl;
cout << sizeof(s3) << " " << sizeof(s4) << endl;
system("pause");
return 0;
}
结果:
4 1
4 4
请按任意键继续. . .