Linux下学C语言——第十二节 代码练习

编程入门 行业动态 更新时间:2024-10-07 05:19:02

Linux下学C语言——<a href=https://www.elefans.com/category/jswz/34/1618982.html style=第十二节 代码练习"/>

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语言——第十二节 代码练习

本文发布于:2024-02-28 02:16:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1767548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第十二节   语言   代码   Linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!