第十二节 代码练习"/>
Linux下学C语言——第十二节 代码练习
学生信息的添加,修改,展示
#include <stdio.h>#include <stdlib.h>#include <string.h>#define NAMESIZE 32struct student_st{int id;char name[NAMESIZE];int math;int chinese;};void stu_changename(struct student_st *p,const char *newname ){strcpy(p->name,newname);}void stu_set(struct student_st *p,struct student_st *q){*p = *q;}void stu_show(struct student_st *p){printf("%d,%s,%d,%d\n",p->id,p->name,p->math,p->chinese);}void menu (void){printf("\n1 set\n2 change\n3 show\n");printf("please enter num:(q for quit)\n");}int main(){struct student_st stu,tmp;char newname[NAMESIZE];int choice;do{ menu();if(scanf("%d",&choice)!=1)break;switch(choice){case 1:printf("please enter for the stu[id name math chinese]\n");scanf("%d%s%d%d",&tmp.id,tmp.name,&tmp.math,&tmp.chinese);stu_set(&stu,&tmp);break;case 2:printf("please enter the newname:\n");scanf("%s",newname);stu_changename(&stu,newname);break;case 3:stu_show(&stu);break;default:exit(1);}}while(1);}
使用union和struct嵌套实现数字的前十六位和后十六位求和;
#include <stdio.h>#include <stdlib.h>#include <stdint.h>union{struct//union嵌套了一个struct{uint16_t i;uint16_t j;}x;uint32_t y;}a;int main(){a.y = 0x11223344;printf("%x\n",a.x.i+a.x.j);exit(0);}
执行
[tom@CentOS7 struct]$ ./union
4466
更多推荐
Linux下学C语言——第十二节 代码练习
发布评论