C错题练习

来源:互联网 发布:苍空的解放者 知乎 编辑:程序博客网 时间:2024/05/16 23:55

1、strlen

#include <iostream>using namespace std;int main(){char p[]={'a','b','c'},q[10]={'a','b','c'};printf("%d %d\n",strlen(p),strlen(q));//由于p数组中没有字符串结束符,长度不能确定,q数组中字符串长度为3for(int i=0;i<10;i++){cout<<(int)p[i]<<'\t'<<(int)q[i]<<endl;}system("pause");return 0;}
输出:

2、strlen

#include <iostream>using namespace std;void f(char *p[],int n){char *t;int i,j;for(i=0;i<n-1;i++)for(j=i+1;j<n;j++)if(strcmp(p[i],p[j])>0){t = p[i];p[i] = p[j];p[j] = t;}}int main(){char *p[5] = {"abc","aabdfg","abbd","dcdbe","cd"};f(p,5);printf("strlen(p[1]) = %d\n",strlen(p[1]));for(int i = 0;i<5;i++){puts(p[i]);}system("pause");return 0;}
输出:

3、switch

int main(){int k = 5,n = 0;while(k>0){switch(k){default:break;case 1:n+=k;case 2:case 3:n+=k;}k--;}printf("%d\n",n);//输出7/*第一次k=5,进入while,switch,不满足执行break跳出switch语句,k--后,k=4.第二次k=4,k>0,进入while,switch不满足,break,k--,k=3.第三次k=3,k>0,进入while,switch满足n=n+k;n==3;k--,k=2;第四次k=2,k>0,进入while,switch中case 2无break语句,接着执行后续语句,n=3+2=5,k--,k=1.第五次k=1,k>0,进入while,switch中case 1满足无break语句,n=5+1=6,n=6+1=7,k--,k=0.第六次k=0,k>0不满足,跳出while循环。*/system("pause");return 0;}
4、getchar(),gets()




0 0
原创粉丝点击