结构体,联合体,枚举和宏函数

结构体

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值