#include<stdio.h> //switch 日期例子 intmain() { int year,mon; while(scanf("%d%d",&year,&mon)) { switch (mon) { case2:printf("mon=%d is %d days\n",mon,28+(year%4==0&&year%100!=0|| year%400==0));break; case1:printf("mon=%d is 31days\n",mon);break; case3:printf("mon=%d is 31days\n",mon);break; case5:printf("mon=%d is 31days\n",mon);break; case7:printf("mon=%d is 31days\n",mon);break; case8:printf("mon=%d is 31days\n",mon);break; case10:printf("mon=%d is 31days\n",mon);break; case12:printf("mon=%d is 31days\n",mon);break; case4:printf("mon=%d is 30days\n",mon);break; case6:printf("mon=%d is 30days\n",mon);break; case9:printf("mon=%d is 30days\n",mon);break; case11:printf("mon=%d is 30days\n",mon);break; default: printf("error mon\n"); } } return0; }
do-while
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdio.h>
intmain(){ int i=1,total=0; do{ total+=i; i++; }while(i<=100);//必须有分号,否则编译不通 printf("total=%d\n",total); return0; }
5、二维数组,二级指针讲解
二维数组
1 2 3 4 5 6 7 8 9
#include<stdio.h>
intmain(){ //通过调试查看元素存放顺序 int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12 }; printf("sizeof(a)=%d\n",sizeof(a)); printf("a[2][3]=%d\n", a[2][3]);//最后一个元素是a[2][3] return0; }
二级指针
**p2 ~ *p ~ i ~ 10
*p2 ~ p ~ &i 存的是i的地址
P2 ~ &p 存的是p的地址
1 2 3 4 5 6 7 8 9 10
#include<stdio.h> //二级指针的理解 intmain(){ int i=10; int *p=&i; int **p2=&p;//如果我们需要把一个一级指针变量的地址存起来,那么就需要二级指针类型 printf("sizeof(p2)=%d\n",sizeof(p2));//p2和p同样大,都是8个字节 printf("**p2=%d\n",**p2);//通过两次取值可以拿到i return0; }