类型别名 typedef
c
#include <stdio.h>
// Rust style type alias
typedef char i8;
typedef short i16;
typedef int i32;
typedef long i64;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;
typedef char* string;
int main() {
i8 character = 'A'; // 本质就是 char
u8 number = 255; // 本质就是 unsigned char
string str = "hello world"; // 本质就是 char*
printf("Size of i8 : %zu bytes\n", sizeof(i8));
printf("Size of i16: %zu bytes\n", sizeof(i16));
printf("Size of i32: %zu bytes\n", sizeof(i32));
printf("Size of i64: %zu bytes\n", sizeof(i64));
}
枚举类型 enum
在人类的概念中, 有很多东西是有限的且不会变更的(比如:季节只有4个,一周只有7天),此时就可以使用枚举来提升代码可读性
c
#include <stdio.h>
// 枚举所有的值都是 int 类型
enum SEASONS {
// 如果不指定值, 默认从0开始, 后续递增1
SPRING,
SUMMER,
AUTUMN,
WINTER
};
enum WEEK {
// 如果指定一个值, 从指定的值开始, 后续递增1
MONDAY = 1,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
int main() {
enum SEASONS spring = SPRING;
printf("SEASONS start with: %d\n", SPRING);
printf("SPRING: %d \n", SPRING);
printf("SUMMER: %d \n", SUMMER);
printf("AUTUMN: %d \n", AUTUMN);
printf("WINTER: %d \n", WINTER);
printf("\n");
enum WEEK monday = MONDAY;
printf("WEEK start with: %d \n", monday);
printf("MONDAY : %d \n", MONDAY);
printf("TUESDAY : %d \n", TUESDAY);
printf("WEDNESDAY : %d \n", WEDNESDAY);
printf("THURSDAY : %d \n", THURSDAY);
printf("FRIDAY : %d \n", FRIDAY);
printf("SATURDAY : %d \n", SATURDAY);
printf("SUNDAY : %d \n", SUNDAY);
}