C语言错题集
记录学习c语言路上遇到的各种奇葩
问题
选填易错题
- 在c语言中,一维数组的定义方式为:类型说明符 数组名
[常量表达式]
; - 赋值运算<符逻辑与运算符<关系运算符<算术运算符
- 一个c语言程序是从
本程序的main函数
开始,到main函数
结束 - 最小生成树代价唯一
- 数字数组长度:
sizeof(数组名)/sizeof(数组类型)
,如:sizeof(arr)/sizeof(int); - c源程序的基本单位是
函数
- 一个c源程序中至少应包含一个
main函数
- 在c语言中,char型数据在内存中的存储形式是
ASCLL码
- 在c程序中,
%
是只能用于整数运算的运算符 - c语言中的实型变量分为两种类型:
单精度型,双精度型
- c语言所提供的基本数据类型包括:
单精度型,双精度型,整型,字符型,枚举型
- 文件由
数据序列
组成,可以构成二进制文件
或文本文件
- 串是一种特殊的线性表,其特殊性体现在:
数据元素类型是字符型
- 线性表L在
很少进行插入和删除运算
情况下,使用顺序存储更合适 - 算法五特性:
有穷性,确定性,可行性,输入,输出
- 好算法特性:
正确性,可读性,健壮性,高效率和低存储
- 有n个顶点的强连通图至少有
n
条边 - 根据元素间关系的不同,通常可以将数据结构分为:
树形结构,集合结构,线性结构,图状结构
字符 | ascll码表 |
---|---|
0 | 48 |
1 | 49 |
2 | 50 |
3 | 51 |
4 | 52 |
5 | 53 |
6 | 54 |
7 | 55 |
8 | 56 |
9 | 57 |
A | 65 |
a | 97 |
类型 | 长度 |
---|---|
char | 1 |
short | 2 |
int | 4 |
long | 4 |
float | 4 |
double | 8 |
c语言关键字
typedef struct union enum
void return
static const signed unsigned short int long float double char
if else switch case default break continue for do while
auto goto sizeof register extern volatile
错题篇
//字符转换:y->A,z->B
void err(){
char c;
while ((c=getchar())!='\n'){
if(c>='a'&&c<='z'){
c-=30;
if(c>'Z'&&c<='Z'+2)
c-=26;//这个位置出错了
}
printf("%c",c);
}
}
void err1(){
//输入:2473回车
//则输出的内容是:668977
char c;
while ((c=getchar())!='\n'){
switch (c-'2') {
case 0:
case 1:
putchar(c+4);
case 2:
putchar(c+4);
break;
case 3:
putchar(c+3);
default:
putchar(c+2);
break;
}
printf("\n");
}
}
int i;
for(i=1;i++<4;)
最后i的值是5
void err4(){
// 如何赋值,可以让他们一次是:10,20,A,B
// 答案:10 20AB(回车)
int a1,a2;
char c1,c2;
scanf("%d%d",&a1,&a2);
scanf("%c%c",&c1,&c2);
printf("%d,%d,%c,%c",a1,a2,c1,c2);
}
void err5(){
// 依次输入:12345 678
// 则输出:123,45.000000
int x;
float y;
scanf("%3d%f",&x,&y);
printf("%d,%f",x,y);
}
void err6(){
// 输出一个正等差数列的前十项,此数列前四项之和是26,积分是880
int a,d,i,s,f,x;
for ( a = 1; a <30; ++a)
for ( d = 1; d <=5; ++d) {//这里出错,写的d<=a
s = 0;
f = 1;
x = a;
for (i = 1; i <= 4; ++i) {
s += x;
f *= x;
x += d;
}
if (s == 26 && f == 880)
for (int i = 0; i < 10; ++i)
printf("%3d", a + i * d);
}
printf("\n");
}
void err7(){
// 此程序的运行结果是:8,-2
// 我写的:8,-1
int i,b,k=0;
for ( i = 1; i <= 5; ++i) {
b=i%2;
while (b-->=0) k++;
}
printf("%d,%d",k,b);
}
void err8(){
// 这个程序不是死循环,i加到一定值会变成负数
int i=32767;
do {
if(i<0)
break;
} while (++i);
}
void err9(){
// 运行结果:k=4
int k=0;
char c='A';
do {
switch (c++) {
case 'A': k++;break;
case 'B':k--;
case 'C':k+=2;break;
case 'D':k=k%2;continue;
case 'E':k=k*10;break;
default: k=k/3;
}
k++;
} while (c<'G');
printf("k=%d",k);
}
void err10(){
// 程序的输出结果是:4
int i,j,a=0;
for ( i = 0; i <2; ++i) {
for ( j = 0; j < 4; ++j) {
if(j%2)
break;
a++;
}
a++;
}
printf("%d\n",a);
}
void err11(){
// 这里的输出结果是-1
// for循环中的判断条件不成立
int a,i;
for (i = -1,a=1; -1<=i<1; ++i) {
a++;
printf("%2d",a);
}
printf("%2d",i);
}
void err12(){
// 运行结果:1 2 3 default 4
int a=1;
switch (a) {
case 1:
printf("1 ");
case 2:
printf("2 ");
case 3:
printf("3 ",a);
default:
printf("default ");
case 4:
printf("4 ");
}
// 运行结果:default 4
int a2=5;
switch (a2) {
case 1:
printf("1 ");
case 2:
printf("2 ");
case 3:
printf("3 ",a2);
default:
printf("default ");
case 4:
printf("4 ");
}
}
_Bool delchar(char s[],char c){
// 删除字符串中指定字符
int i=0,j=0;
while (s[j]!='\0'){
if(s[j]!=c){
s[i++]=s[j];
}
j++;
}
s[i]='\0';
return 1;
}
void err14(){
// 输出:Prohsbn
char a[]={"Program"},*p;
p=&a[3];
char *q;
q=p;
while (*q!='\0'){
(*q)++;
q++;
}
printf("%s\n",a);
}
void err15(int*x,int *y){
// swap()函数的正确写法
int temp=*x;
*x=*y;
*y=temp;
}
void err16(){
// 输出结果:a=2,b=1
int x=1,y=0,a=0,b=0;
switch (x) {
case 1:
switch (y) {
case 0:a++;break;
case 1:b++;break;
}
case 2:
a++;b++;break;
}
printf("a=%d,b=%d",a,b);
}
void err17(){
// 输入:China#
// 输出:5,5
int v1=0,v2=0;
char ch;
while ((ch=getchar())!='#'){
switch (ch) {
case 'a':
case 'h':
default: v1++;
case 'o':v2++;
}
}
printf("%d,%d\n",v1,v2);
}
void err18(){
// 辗转相除法求两个数最大公约数
int r,m,n;
scanf_s("%d,%d",&m,&n);
if(m<n){
r=m;
m=n;
n=r;
}
r=m%n;
while (r){
m=n;
n=r;
r=m%n;
}
printf("%d\n",n);
}
void err19(){
// 求:11的11次方的个十百位上的数字之和
// 11的11次方=285311670611
// 输出:m=8
int i,s=1,m=0;
for (i = 1; i <=11; ++i) {
s=s*11%1000;
}
do{
m+=s%10;
s=s/10;
} while (s);
printf("m=%d\n",m);
}
void err20(){
// 输出:5.500000
int b=7;
float a=2.5,c=4.7;
float d=a+(int)(b/3*(int )(a+c)/2)%4;
printf("%f ",d);
}
void err21(){
// 完成用一块钱换成一分,两分,五分的所有兑换方案
int i,j,k,l=1;
for (i = 0; i <=20 ; ++i) {
for (j = 0; j <=50 ; ++j) {
k=100-i*5-j*2;
if(k>=0){
printf("五分 二分 一分\n");
printf("%d %d %d \n",i,j,k);
l++;
if(l%5==0)
printf("\n");
}
}
}
}
void err22(){
// 判断a-z这26个字符组成的字符串中那个字符出现的次数最多
int cases,sum[26],i,max;
char str[1000];
scanf("%d", &cases);//
while (cases>0) {//
scanf("%s", str);//
for (i = 0; i < 26; ++i) {//
sum[i] = 0;//
}//
for (i = 0; i < strlen(str); ++i){
sum[str[i]-'a']++;//
}
max = 0;
for (i = 1; i < 26; ++i){
if (sum[i]>sum[max])
max = i;
}
printf("%c %d\n", max + 'a', sum[max]);
}
}
void err23(){
// 输出:Prohsbn
char a[]={"Program"},*p;
p=&a[3];
char *q;
q=p;
while (*q!='\0'){
(*q)++;
q++;
}
printf("%s",a);
}
void err24(){
// 输出:2
int i=2.7;
printf("%d,",i);
}
void err25(){
// 输出:3,5
int k=5;
int m=3;
int *s=&m;
s=&k;
*s=k;
printf("%d,%d",m,k);
}
很难理解的内容篇
疑惑1:
void unKnown(){
//求输出结果
int y,x;
for (int y = 1; y <10; ) {
y=((x=3*y,x+1),x-1);
}
printf("%d,%d",x,y);
}
void unKnwn1(){
long y=-43456;
printf("%2ld\n",y);
printf("%3ld\n",y);
printf("%4ld\n",y);
printf("%8ld\n",y);
printf("%-8ld\n",y);
printf("%-08ld\n",y);
printf("%-8ld\n",y);
printf("%08ld\n",y);
printf("%+8ld\n",y);
}
void unKnow2(){
int y=2456;
// %o是八进制
// 补充:%x是十六进制
printf("%o\n",y);
printf("%3o\n",y);
printf("%8o\n",y);
printf("%#8o\n",y);
}
void unKnow3(){
// 数学函数
double y=0.5,z=1.5;
int x=10;
z= exp(y)+ fabs(x);
y= log10(y);
z= sqrt(y-z);
x=(int )(atan2((double )x,y)+ exp(y-0.2));
printf("%d",x);
}
void unKnow4(){
// 检测出来输出的是32783,答案是无法确定输出值
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
printf("x+y+z=%d\n",x+y+z);
}
void unKnow5(){
// 调试不出来
int n=0,c;
c=getchar();
while (c!='\n'){
if(c>=0&&c<=9){
n++;
c=getchar();
}
}
printf("一共输出%d个数字字符",n);
}
void unKnow6(){
// 统计输入数字字符的个数
// 运行不出来来
int n=0,c;
c=getchar();
while(c!='\n'){
if(c>=0&&c<=9)
n++;
c=getchar();
}
}