枚举类型
枚举的定义:
可以使用枚举(enumerated type)声明代表整数常量的符号名称,关键字:enum创建一个新的枚举类型。
事实上,enum常量是int类型的
#include <stdio.h>
#include <stdlib.h>
//枚举
#define red 0
#define black 1
#define yellow 2
enum A{
red1,black1,yellow1//这里就相当于定义了3个常量,从0开始的
};
int main(int argv,char *args[]){
//red1=100;枚举是个常量,是不能修改的
printf("%d,%d,%d\n",red1,black1,yellow1);
return 0;
}
0,1,2
Process finished with exit code 0
如果在定义的时候我们给定了初值:
#include <stdio.h>
#include <stdlib.h>
//枚举
enum A{
red1=5,black1,yellow1
};
int main(int argv,char *args[]){
printf("%d,%d,%d\n",red1,black1,yellow1);
return 0;
}
5,6,7
Process finished with exit code 0
后面的根据前一个依次往下排。
typedef
typedef是一种高级的数据特性,它是能使某一类型创建自己的名字
#include <stdio.h>
typedef char BYTE;//定义了一个新的数据类型,名字是BYTE类型是char,这里是一个标准的数据类型
#define BYTE1 char //预编译指令,只是作语法替换,
int main(int argv,char *args[]){
char c;
BYTE a;
BYTE1 a1;
a=10;
printf("%d\n",a);
return 0;
}
#include <stdio.h>
//为了方便兼容(微软)
typedef int INT ;
typedef unsigned int UINT;
typedef char CHAR ;
typedef unsigned char UCHAR;
//在下面所用到如int类型,直接改变typedef int INT ;这里,将int替换其他类型就恶意,方便移植。不必改下面的每个int
//改成typedef long INT ;
//结构体的替换
struct abc{
int age_1;
char b;
};
typedef struct abc A ;
typedef struct {
int a1;
char b1;
} A1;
int main(){
A a;
A1 a_1;
return 0;
}
在使用方便上,可以将结构体的定义“替换”
在定义一个数据类型的时候,我们使用typedef
在定义一个常量的时候,我们使用#define
typedef比define更灵活
通过typedef定义函数指针
指向函数的指针:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *mystrcat(char *s1,char *s2){
strcat(s1,s2);
return s1;
}
typedef char *(*STRCAT)(char *s1,char *s2);
//char *test(char *(*p)(char *s1,char *s2),char *s1,char *s2){
char *test(STRCAT p,char *s1,char *s2){
return p(s1,s2);
}
int main(int argv,char *args[]){
char s1[100]="hello";
char s2[100]="world";
//char *s=mystrcat(s1,s2);
char *s=test(mystrcat,s1,s2);
printf("s=%s\n",s);
return 0;
}