您好,欢迎访问三七文档
数组的引用:#includestdio.hvoidmain(intargc,char*argv[]){intx[5]={2,3,1,4,6};inti;for(i=0;i5;i++){printf(%d\n,2*x[i]);}}一维数组的遍历:#includestdio.hvoidmain(intargc,char*argv[]){intx[5]={1,2,3,4,5};inti=0;for(i=0;i5;i++){printf(x[%d]:%d\n,i,x[i]);}}一维数组的最值:#includestdio.hintmain(intargc,char*argv[]){intx[5]={1,2,3,4,5};intnMax=x[0];inti=0;for(i=1;i5;i++){if(x[i]nMax){nMax=x[i];}}printf(max:%d\n,nMax);return0;}一维数组排序:#includestdio.hvoidmain(intargc,char**argv[]){intarr[5]={9,8,3,5,2};inttemp=0;inti,j;printf(冒泡排序前:\n);for(i=0;i5;i++){printf(%d,arr[i]);}printf(\n);for(i=0;i5-1;i++){for(j=0;j5-1-i;j++){if(arr[j]arr[j+1]){temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}printf(冒泡排序后:\n);for(i=0;i5;i++){printf(%d,arr[i]);}printf(\n);}二维数组的遍历#includestdio.hvoidmain(){//声明并初始化数组intarray[3][4]={{1,2,3,4,},{5,6,7,8,},{9,10,11,12}};for(inti=0;i3;i++)//循环遍历行{for(intj=0;j4;j++)//循环遍历列{printf([%d][%d]:%d,i,j,array[i][j]);}printf(\n);//每一行的末尾添加换行符}}二维数组应用#includestdio.hvoidmain(intargc,char*argv[]){intStuScore[5][3]={//张同学{88,70,90},//王同学{80,80,60},//李同学{89,60,85},//赵同学{80,75,78},//周同学{70,80,80},};intm=0,n=0;intnStuTotalScore;intnMathTotalScore=0;intnChineseTotalScore=0;intnEnglishTotalScore=0;printf(个人总成绩:\n);for(m=0;m5;m++){nStuTotalScore=0;for(n=0;n3;n++){nStuTotalScore+=StuScore[m][n];switch(n){case0:{nMathTotalScore+=StuScore[m][0];break;}case1:{nChineseTotalScore+=StuScore[m][1];break;}case2:{nEnglishTotalScore+=StuScore[m][2];}}}switch(m){case0:{printf(张同学:%d\n,nStuTotalScore);break;}case1:{printf(王同学:%d\n,nStuTotalScore);break;}case2:{printf(李同学:%d\n,nStuTotalScore);break;}case3:{printf(赵同学:%d\n,nStuTotalScore);break;}case4:{printf(周同学:%d\n,nStuTotalScore);break;}}}printf(小组数学总分:%d小组数学平均分:%.2f\n,nMathTotalScore,(double)nMathTotalScore/5);printf(小组语文总分:%d小组语文平均分:%.2f\n,nChineseTotalScore,(double)nChineseTotalScore/5);printf(小组英语总分:%d小组英语平均分:%.2f\n,nEnglishTotalScore,(double)nEnglishTotalScore/5);}无参函数#includestdio.hvoidfunc(){printf(这是我的第一个函数!\n);}voidmain(){func();}有参函数调用无参函数#includestdio.hvoidfunc(intx,inty){intsum=x+y;printf(x+y=%d\n,sum);}voidmain(){func(3,5);}函数调用及返回过程#includestdio.hintfunc(intx,inty){returnx+y;}voidmain(){intsum=func(3,5);printf(x+y=%d\n,sum);}Printf函数#includestdio.hvoidmain(){printf(%c%c,'H','\n');printf(%s,Hellow,world!\n);printf(%d%d%d\n,1,2,3);printf(%f%f%f\n,2.1,2.2);}Scanf函数#includestdio.hvoidmain(){charstr[256];//字符数组保存得到的字符串scanf(%s,str);printf(%s\n,str);}嵌套函数#includestdio.hintmax2(inta,intb)//用于比较两个整数大小的函数{if(ab){returna;}else{returnb;}}intmax4(inta,intb,intc,intd)//用于比较四个整数大小的函数{intres;res=max2(a,b);res=max2(res,c);res=max2(res,d);returnres;}voidmain(){inta=5,b=7,c=9,d=22,max;printf(请输入四个整数:57922\n);//scanf(%d%d%d%d,a=5,b=7,c=9,d=22);max=max4(a,b,c,d);printf(max=%d\n,max);}递归调用#includestdio.h//下面的函数使用递归实现,求1~n的和intgetsum(intn){if(n==1){return1;//满足条件,递归结束}inttemp=getsum(n-1);//在函数体中调用自身returntemp+n;}voidmain(){intsum=getsum(4);//调用递归函数,获得1~4的和printf(sum=%d\n,sum);//打印结果}外部函数建立源文件first.cintadd(intx,inty){returnx+y;}建立源文件second.c#includestdio.hexternintadd(intx,inty);voidmain(){printf(%d\n,add(1,2));}//注:extern可以省略内部函数用法建立first.c#includestdio.hvoidshow(){printf(%s\n,first.c);}建立second.c#includestdio.hstaticvoidshow(){printf(%s\n,second.c);}voidmain(){show();}全局变量#includestdio.hintx=123;voidshow(){printf(show()函数中,x的值为:%d\n,x);}voidmain(){show();printf(main()函数中,x的值为:%d\n,x);}当局部变量与全局变量重名时,全局变量会被屏蔽#includestdio.hintx=789;voidmain(){intx=123;printf(x的值为:%d\n,x);}变量的作用域#includestdio.hintx=10;voidshow(){intx=20;printf(show()函数中,x的值为:%d\n,x);}voidmain(){intx=30;printf(main()函数中,x的值为%d\n,x);show();}If语句#includestdio.hvoidmain(){//定义变量xintx=5;//如果x的值小于10则执行if大括号中的代码if(x10){x++;}printf(x=%d\n,x);}If...else语句#includestdio.hvoidmain(){intnum=19;if(num%2==0){//判断条件成立,num被2整除printf(num是一个偶数\n);}else{printf(num是一个奇数\n);}}If...elseif...else语句#includestdio.hvoidmain(){intgrade=75;//定义学生成绩if(grade=80){//满足条件grsde=80printf(该成绩的等级为优\n);}elseif(grade=70){//不满足条件grade=80,但满足条件grade=70printf(该成绩等级为良\n);}elseif(grade=60){//不满足条件grade=70,但满足条件grade=60printf(该成绩等级为中\n);}else{//不满足条件grade=60printf(该成绩的等级为差\n);}}三元运算符#includestdio.hvoidmain(){intx=0;inty=1;intmax;max=xy?x:y;printf(max=%d\n);}Switch语句#includestdio.hvoidmain(){intweek=5;switch(week){case1:printf(星期一\n);break;case2:printf(星期二\n);break;case3:printf(星期三\n);break;case4:printf(星期四\n);break;case5:printf(星期五\n);break;case6:printf(星期六\n);break;case7:printf(星期日\n);break;default:printf(输入的数字不正确...\n);break;}}在使用switch语句过程中,如果多个case条件后面的执行语句是一样的,则该执行语句只需书写一次即可#includestdio.hvoidmain(){intweek=2;switch(week){case1:case2:case3:case4:case5://当week满足值1、2、3、4、5中任意一个时,处理方式相同printf(今天是工作日\n);break;case6:case7://当week满足值6、7中任意一个时,处理方式相同printf(今天是休息日\n);break;}}While循环语句#includestdio.hvoidmain(){intx=1;while(x=4){printf(x=%d\n,x);//条件成立,打印x的值x++;//x进行自增}}Do-while循环语句#includestdio.hvoidmain(){intx=1;//定义变量x,初始值为1do{printf(x=%d\n,x);//条件成立,打印x的值x++;//x进行自增}while(x=4);//循环条件}For循环#includestdio.hvoidma
本文标题:C语言程序实例
链接地址:https://www.777doc.com/doc-226804 .html