1. 什么是类型定义
- typedef 是一个高级数据特性,它可以为某一类型自定义名称, 即类型的别名。
2. 为什么要使用类型定义
- 简化写法
- 提高程序的可移植性
//在 64 位 linux 系统下编译运行
#include <stdio.h>
#include <stdlib.h>
typedef long int64;
int main(void){
int64 dream = 10000000000; //梦想一百亿
printf("dream: %lld\n", dream); //输出10000000000
printf("sizeof(int64): %d\n", sizeof(int64)); //输出8字节
return 0;
}
打印结果 dream: 10000000000
sizeof(int64): 8
//在 32 window 系统编译运行
#include <stdio.h>
#include <stdlib.h>
typedef long long int64;
int main(void){
int64 dream = 10000000000; //梦想一百亿
printf("dream: %lld\n", dream); //输出10000000000
printf("sizeof(int64): %d\n", sizeof(int64)); //输出8字节
return 0;
}
打印结果 dream: 10000000000
sizeof(int64): 8
3. 类型定义的使用
#include <stdio.h>
#include <stdlib.h>
typedef char * STRING;
#define STR char *
int main(void){
STRING s1, s2; //等同于 char *s1; char *s2;
char name[] = "Martin";
s1 = name;
s2 = name;
STR s3, s4; // char * s3, s4;
s3 = name;
s4 = name[0];
system("pause");
return 0;
}
类型定义和宏定义有相似之处,但不能混为一谈