结构体
#include<stdio.h>
#include<string.h>
struct student //声明结构体
{
char name[20];
int age;
char sex;
};
int main()
{
struct student s1;
strcpy(s1.name,"hong");
s1.age=18;
s1.sex='f';
struct student s2;
strcpy(s2.name,"hei");
s2.age=19;
s2.sex='m';
printf("%s,%d,%c\n",s1.name,s1.age,s1.sex);
printf("%s,%d,%c\n",s2.name,s2.age,s2.sex);
return 0;
}
联合体
#include <stdio.h>
union test
// 共享同一内存
{
int a;
int b;
char c;
};
int main()
{
union test t1;
t1.a=100;
printf("%d\n",t1.b);
t1.a=0x10101010;
printf("%x\n",t1.c);
}
端子节序判断
#include <stdio.h>
union Byte
{
short val;
char ch[sizeof(short)];
};
int main()
{
union Byte s;
s.val=0x0102;
if(s.ch[0]==1 && s.ch[1]==2)
{
printf("大端字节序\n");
}
if(s.ch[0]==2 && s.ch[1]==1)
{
printf("小端字节序\n");
}
return 0;
}
枚举
#include<stdio.h>
enum test
//从0开始递增
{
a,
b,
c,
d=13,//往下从13开始递增
e,
f
};
int main()
{
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
printf("%d\n",d);
printf("%d\n",e);
printf("%d\n",f);
return 0;
}
宏函数
#include<stdio.h>
#define PRINT printf("helloworld\n")
#define P(s) printf("%s",s)
#define SQR1(x) (x*x)
#define SQR2(x) ((x)*(x))
/*
宏函数优点:执行效率高 节省空间
宏函数缺点:没有语法检查(不安全) 编译效率低
*/
int main()
{
PRINT;
int a=1,b=2;
printf("%d\n",SQR1(a+b));//1+2*1+2
printf("%d\n",SQR2(a+b));//(1+2)*(1+2)
return 0;
}