0816学习笔记(常见错误分析)

1、忘记定义变量(会报错)

#include <stdio.h>

int main()
{
	int c;
	c=x+y;
	printf("%d",c);
	
	return 0;
}

2、输入输出类型不一致(不报错,最多会警告)

#include <stdio.h>

int main()
{
	int c;
	c=6+3;
	printf("%f",c);
	
	return 0;
}

结果是0.000000

3、超出数据范围

#include <stdio.h>

int main()
{
	int c;
	c=20000000000;
	printf("%d",c);
	
	return 0;
}

结果

-1474836480
--------------------------------
Process exited after 0.4559 seconds with return value 0
请按任意键继续. . .

4、使用scanf忘记符号&或多用(不报错)

#include <stdio.h>

int main()
{
	int c;
	scanf("%d",c);
	printf("%d",c);
	int *p=&c;
	scanf("%d",&p);
	printf("%d",*p);
	
	return 0;
}

结果

45

--------------------------------
Process exited after 2.355 seconds with return value 3221225477
请按任意键继续. . .

5、scanf输入形式与要求不符

#include <stdio.h>

int main()
{
	int c,b;
	scanf("%d%d",&c,&b);
	printf("%d%d",c,b);

	
	return 0;
}

结果

2,3
21
--------------------------------
Process exited after 3.381 seconds with return value 0
请按任意键继续. . .

6、scanf向数组输入

#include <stdio.h>

int main()
{
	char arr[20];
	scanf("%s",&arr);//不需要取地址,多余 
	puts(arr); 
	int arr_1[20];
	scanf("%d",arr_1);//数值赋值只能循环单个赋值 
	

	
	return 0;
}

7、预处理指令当c语句处理,后面加“;”

#include <stdio.h>;

8、不该加“;”的地方加了“;”

#include <stdio.h>

int main()
{ 
	int arr_1[20];
	int i;
	for(i=0;i<5;i++);
	{
		scanf("%d",&arr_1[i]);//数值赋值只能循环单个赋值 
	}
	if(arr_1[2]>0);
	{
		printf("%d",arr_1[2]);
	}
	switch(i);
	{
		...;
	}

	
	return 0;
}

9、漏掉{}

#include <stdio.h>

int main()
{ 
	int arr_1[20];
	int i;
	for(i=0;i<5;i++);
	{
		scanf("%d",&arr_1[i]);//数值赋值只能循环单个赋值 
	}
	if(arr_1[2]>0);
	{
		printf("%d",arr_1[2]);
	
	switch(i);
	{
		...;
	}

	
	return 0;
}

10、()不配对

	while((c=getchar()==0)
	{
		...;
	}

11、标识符大小写混用(会报错)

#include <stdio.h>

int main()
{ 
	int arr_1[20];
	int i; 
	for(I=0;I<5;I++)
	{
		scanf("%d",&arr_1[I]);//数值赋值只能循环单个赋值 
	}

	
	return 0;
}

12、赋值(=)和相等(==)混用

	if(arr_1[2]=1)
	{
		...;
	}
	

13、使用数组时,使用下标为定义的最大下标

#include <stdio.h>

int main()
{ 
	int arr_1[20];
	int i; 
	for(i=0;i<5;i++)
	{
		scanf("%d",&arr_1[i]);//数值赋值只能循环单个赋值 
	}
	printf("%d",arr_1[20]);//越界 
	
	return 0;
}

14、误认为数组名代表数组的所有元素

#include <stdio.h>

int main()
{ 
	int arr_1[20];
	int i; 
	for(i=0;i<5;i++)
	{
		scanf("%d",&arr_1[i]);//数值赋值只能循环单个赋值 
	}
	printf("%d",arr_1[20]);//越界 
	int b[20];
	b=arr_1;//错误 
	return 0;
}

15、混淆字符数组和字符指针(会报错)

#include <stdio.h>

int main()
{ 
	char a[]="shanshanerchuan";
	a="buyaneryu";//错误 
	char *b="wohewodezuguo";
	b="woshibushinizuitengaideren"; //可以 
	return 0;
}

16、引用指针变量前未指向

#include <stdio.h>

int main()
{ 
	int *p;
	scanf("%d",p); 
	return 0;
}

17、switch语句忘记break

#include <stdio.h>

int main()
{ 
	int p;
	scanf("%d",&p); 
	switch(p)
	{
		case 1:
			...;
		case 2:
			...;
			
	}
	
	return 0;
}

18、混淆字符和字符串

#include <stdio.h>

int main()
{ 
	'a';//字符 
	'abc';//错误 
	"a";//字符串 
	"abc";//字符串 
	
	return 0;
}

19、使用自加(++)自减(--)容易错误

#include <stdio.h>

int main()
{ 
	int a=10;
	a++;
	printf("%d",a++);//a=11而不是12 
	
	return 0;
}

20、忘记对函数声明

#include <stdio.h>

int main()
{ 
	int a=10;
	a++;
	fun(a); 
	
	return 0;
}

void fun(int a)
{
	printf("%d",a);
}

21、函数声明与函数定义不匹配

#include <stdio.h>
int fun(int a);

int main()
{ 
	int a=10;
	a++;
	fun(a); 
	
	return 0;
}

void fun(float a)
{
	printf("%d",a);
}

22、函数的实参和形参类型不一致

#include <stdio.h>
void fun(float a);

int main()
{ 
	int a=10;
	a++;
	fun(a); 
	
	return 0;
}

void fun(float a)
{
	printf("%d",a);
}

23、不同类型指针混用

#include <stdio.h>


int main()
{ 
	int *p,i=500;
	char *s,a=10;
	p=&a;
	s=&i;
	printf("%d %d",*p,*s);
	
	return 0;
}

结果

128010 -12
--------------------------------
Process exited after 0.3635 seconds with return value 0
请按任意键继续. . .

24、系统对函数参数的求值方法

#include <stdio.h>

void fun(int a,int b,int c)
{
	printf("%d %d %d",a,b,c);
}

int main()
{ 
	int i=500;
	char a=10;
	fun(i,i++,++i);
	
	return 0;
}

结果

502 501 502
--------------------------------
Process exited after 0.4191 seconds with return value 0
请按任意键继续. . .

25、混用数组名和指针变量

#include <stdio.h>


int main()
{ 
	int a[20];
	int i;
	int *p=a;
	for(i=0;i<20;i++)
	{
		scanf("%d",a++);
	}
	for(i=0;i<20;i++)
	{
		scanf("%d",p++);
	}
	
	return 0;
}

26、混用结构体类型与结构体变量的区别

struct node 
{
    char name[20];
}s;

 struct nade 是结构体类姓名  

s 是结构体变量

27、混用结构体普通变量和结构体指针变量引用的区别

struct node 
{
    char name[20];
}s;
s.name // 结构变量访问成员的方式
s->name //这样是错误的
struct node * p;
p->name // 结构体指针访问结构体成员的方式
(*p).name // 当然这样也可以 一般人不这样用

28、使用文件时忘记打开,或打开方式与使用情况不匹配

#include <stdio.h>


int main()
{ 
	FILE *fp=NULL;
	if((fp=fopen("D:\zhaozhuang\练习\0816\test.txt","w")==NULL))
	{
		printf("文件不存在。"); 
	}
	else
	{
		char ch;
		ch=fgetc(fp);
	}
	return 0;
}

29、打开文件时,找不到指定文件

#include <stdio.h>


int main()
{ 
	FILE *fp=NULL;
	if((fp=fopen("D:\zhaozhuang\练习\08222","R")==NULL))
	{
		printf("文件不存在。"); 
	}
	return 0;
}

30、忘记关闭文件,虽然程序结束后会关闭文件,但可能丢失数据

 

#include <stdio.h>
#include <string.h>

int main()
{ 
	FILE *fp=NULL;
	fp=fopen("test.txt","w");
	fwrite("山有木兮木有枝",1,16,fp);
	
	
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值