c语言常见错误

c语言常见错误
c语言常见错误

C 语言常见错误分析汇总 (2)

1、在一个工程里出现两个main 函数时 (2)

2、 fatal error C1010: (2)

3、拼写错误 (2)

4、书写标识符时,忽略了大小写字母的区别。 (2)

5、忽略了变量的类型,进行了不合法的运算。 (3)

6、将字符常量与字符串常量混淆。 (3)

7、忽略了“=”与“==”的区别。 (3)

8、忘记加分号。 (3)

9、多加了分号。 (4)

10、 LINK : fatal error LNK1168: cannot open Debug/P1.exe for writing 连接错误:不能打开P1.exe 文件,以改写内容。(一般是P1.Exe 还在运行,未关闭,关闭运行界面即可) (4)

11、 warning C4553: (4)

12、 warning C4700: (5)

13、error C2018: unknown character '0xa3' (5)

14.输入变量时忘记加地址运算符“&”。 (5)

15.输入数据的方式与要求不符。 (5)

16.输入字符的格式与要求不一致。 (5)

17.输入输出的数据类型与所用格式说明符不一致。 (5)

18.switch 语句中漏写break 语句。 (6)

19.忽视了while 和do-while 语句在细节上的区别。 (6)

20.定义数组时误用变量。 (6)

21.在定义数组时,将定义的“元素个数”误认为是可使的最大下标值。 (7)

22.初始化数组时,未使用静态存储。 (7)

23.在不应加地址运算符&的位置加了地址运算符。 (7)

24.同时定义了形参和函数中的局部变量。 (7)

l C 语言常见的出错信息 (8)

最常见的20 种VC++编译错误信息 (14)

C 语言常见错误分析汇总

1、在一个工程里出现两个main 函数时

3.obj : error LNK2005: _main already defined in file1.obj

Debug/HELLO.exe : fatal error LNK1169: one or more multiply defined symbols found 这个就是说,你的main 函数重定义了。你看看是不是你的工程里面,包含了很多个有main 函数的文件?

2、 fatal error C1010:

unexpected end of file while looking for precompiled header directive 出现这个错误的原因是,工程类型建错了,应该选择win 32 console application 3、拼写错误

(1)#include //头文件拼写错了

void main()

{

printf("doeifweofupwp");

}

fatal error C1083: Cannot open include file: 'stido.h': No such file or directory

(2)#inculde //include 拼写错了

void main() {

printf("doeifweofupwp");

}

fatal error C1021: invalid preprocessor command 'inculde' (无效的预编译命令 inculde)

(3)#include void mian() //main 拼写错了

{

printf("doeifweofupwp");

}

error LNK2001: unresolved external symbol _main

Debug/HELLO.exe : fatal error LNK1120: 1 unresolved externals

你用vc 建了一个控制台程序,它的入口函数应该是main.

4、书写标识符时,忽略了大小写字母的区别。

main() { int a=5; printf("%d",A); }

error C2065: 'A' : undeclared identifier(A: 是没有定义/声明的标示符)编译程序把a 和A 认为是两个不同的变量名,而显示出错信息。C 认为大写字母和小写字母是两个不同的字符。习惯上,符号常量名用大写,变量名用小写表示,以增加可读性。

5、忽略了变量的类型,进行了不合法的运算。

main() {

float a,b; printf("%d",a%b);

}

error C2296: '%' : illegal, left operand has type 'float '

error C2297: '%' : illegal, right operand has type 'float '

%是求余运算,得到 a/b 的整余数。整型变量 a 和 b 可以进行求余运算,而实型变量则不允许进行“求余”运算。

6、将字符常量与字符串常量混淆。

char c; c="a";

warning C4047: '=' : 'char ' differs in levels of indirection from 'char [2]'

在这里就混淆了字符常量与字符串常量,字符常量是由一对单引号括起来的单个字符,字符串常量是一对双引号括起来的字符序列。C 规定以“\”作字符串结束标志,它是由系统自动加上的,所以字符串“a”实际上包含两个字符:‘a'和‘\',而把它赋给一个字符变量是不行的。

7、忽略了“=”与“==”的区别。

在许多高级语言中,用“=”符号作为关系运算符“等于”。如在BASIC 程序中可以写if (a=3) then … 但C 语言中,“=”是赋值运算符,“==”是关系运算符。如:

if (a==3) a=b;

前者是进行比较,a 是否和3 相等,后者表示如果a 和3 相等,把b 值赋给a。由于习惯问题,初学者往往会犯这样的错误。

8、忘记加分号。

分号是C 语句中不可缺少的一部分,语句末尾必须有分号。

#include void main() {

int a,b;

a=1

b=2

printf("%d,%d",a,b);

}

error syntax error : missing ';' before identifier 'b' error C2146: syntax error : missing ';' before identifier 'printf'

编译时,编译程序在“a=1”后面没发现分号,就把下一行“b=2”也作为上一行语句的一部分,这就会出现语法错误。改错时,有时在被指出有错的一行中未发现错误,就需要看一下上一行是否漏掉了分号。 {

z=x+y; t=z/100; printf("%f",t); //此处的分号不能省略

}

对于复合语句来说,最后一个语句中最后的分号不能忽略不写(这是和PASCAL 不同的)。

9、多加了分号。

对于一个复合语句,如:

{

z=x+y; t=z/100;

printf("%f",t);

}; //右花括号后不需要加分号

复合语句的右花括号后不应再加分号,否则将会画蛇添足。又如:

if (a%3==0) ; I++;

本是如果3 整除a,则I 加1。但由于if (a%3==0)后多加了分号,则if 语句到此结束,程序将执行I++语句,即不论3 是否整除a,I 都将自动加1。这种隐含的错误编译系统并不会提出错误信息或警告信息。再如:

for (I=0;I<5;I++);

{

scanf("%d",&x);

printf("%d",x);

}

本意是先后输入5 个数,每输入一个数后再将它输出。由于for()后多加了一个分号,使循环体变为空语句,此时只能输入一个数并输出它。

10、 LINK : fatal error LNK1168: cannot open Debug/P1.exe for writing 连接错误:不能打开P1.exe 文件,以改写内容。(一般是P1.Exe 还在运行,未关闭,关闭运行界面即可)

11、 warning C4553:

'= =' : operator has no effect; did you intend '='?

没有效果的运算符“= =”;是否改为“=”?

12、 warning C4700:

local variable 'bReset' used without having been initialized

局部变量“bReset”没有初始化就使用。

13、error C2018: unknown character '0xa3'

不认识的字符'0xa3'。(一般是汉字或中文标点符号)

14.输入变量时忘记加地址运算符“&”。

int a,b;

scanf("%d%d",a,b);

这是不合法的。

Scanf 函数的作用是:按照a、b 在内存的地址将a、b 的值存进去。“&a”指a 在内存中的地址。若运行程序,则在输入数据后程序会终止。

15.输入数据的方式与要求不符。

①scanf("%d%d",&a,&b); 输入时,不能用逗号作两个数据间的分隔符,如下面输入不合法: 3,4 输入数据时,在两个数据之间以一个或多个空格间隔,也可用回车键,跳格键tab。

②scanf("%d,%d",&a,&b);

C 规定:如果在“格式控制”字符串中除了格式说明以外还有其它字符,则在输入数据时应输入与这些字符相同的字符。

下面输入是合法的: 3,4 此时不用逗号而用空格或其它字符是不对的。 3 4 3:4 又如:

scanf("a=%d,b=%d",&a,&b);

输入应如以下形式: a=3,b=4

16.输入字符的格式与要求不一致。

在用“%c”格式输入字符时,“空格字符”和“转义字符”都作为有效字符输入。 scanf("%c%c%c",&c1,&c2,&c3); 如输入a b c 字符“a”送给c1,字符“ ”送给c2,字符“b”送给c3,因为%c 只要求读入一个字符,后面不需要用空格作为两个字符的间隔。

17.输入输出的数据类型与所用格式说明符不一致。

例如,

a 已定义为整型,

b 定义为实型 Int a=3; float b=4.5; printf("%f%d\n",a,b); 编译时不给出出错信息,但运行结果将与原意不符。这种错误尤其需要注意。

18.switch 语句中漏写break 语句。

例如:

根据考试成绩的等级打印出百分制数段。

switch(grade) {

case 'A':printf("85~100\n");

case 'B':printf("70~84\n");

case 'C':printf("60~69\n");

case 'D':printf("<60\n");

default:printf("error\n");

由于漏写了break 语句,case 只起标号的作用,而不起判断作用。因此,当grade 值为 A 时,printf 函数在执行完第一个语句后接着执行第二、三、四、五个printf 函数语句。正确写法应在每个分支后再加上“break;”。例如 case 'A':printf("85~100\n");break;

19.忽视了while 和do-while 语句在细节上的区别。

(1)main() {

int a=0,I;

scanf("%d",&I);

while(I<=10) {a=a+I; I++; }

printf("%d",a); }

(2)main() {int a=0,I;

scanf("%d",&I);

do {a=a+I; I++; }

while(I<=10);

printf("%d",a); }

可以看到,当输入I 的值小于或等于10 时,二者得到的结果相同。而当I>10 时,二者结果就不同了。因为while 循环是先判断后执行,而do-while 循环是先执行后判断。对于大于 10 的数 while 循环一次也不执行循环体,而 do-while 语句则要执行一次循环体。

20.定义数组时误用变量。

int n;

scanf("%d",&n);

int a[n];

数组名后用方括号括起来的是常量表达式,可以包括常量和符号常量。即 C 不允许对数组的大小作动态定义。

21.在定义数组时,将定义的“元素个数”误认为是可使的最大下标值。

main() {

static int a[10]={1,2,3,4,5,6,7,8,9,10};

printf("%d",a[10]);

}

C 语言规定:定义时用a[10],表示a 数组有10 个元素。其下标值由0 开始,所以数组元素a[10]是不存在的。

22.初始化数组时,未使用静态存储。

int a[3]={0,1,2};

这样初始化数组是不对的。C 语言规定只有静态存储(static)数组和外部存储(exterm)数组才能初始化。应改为: static int a[3]={0,1,2};

23.在不应加地址运算符&的位置加了地址运算符。

scanf("%s",&str);

C 语言编译系统对数组名的处理是:数组名代表该数组的起始地址,且 scanf 函数中的输入项是字符数组名,不必要再加地址符&。应改为:

scanf("%s",str);

24.同时定义了形参和函数中的局部变量。

int max(x,y)

int x,y,z;

{z=x>y?x:y;

return(z);

}

形参应该在函数体外定义,而局部变量应该在函数体内定义。应改为:

int max(x,y)

int x,y;

{

int z;

z=x>y?x:y;

return(z);

}

l C 语言常见的出错信息

Ambiguous operators need parentheses 不明确的运算需要用括号括起

Ambiguous symbol ’xxx’ 不明确的符号

Argument list syntax error 参数表语法错误

Array bounds missing 丢失数组界限符

Array size toolarge 数组尺寸太大

Bad character in paramenters 参数中有不适当的字符

Bad file name format in include directive 包含命令中文件名格式不正确

Bad ifdef directive synatax 编译预处理

ifdef 有语法错

Bad undef directive syntax 编译预处理

undef 有语法错

Bit field too large 位字段太长

Call of non-function 调用未定义的函数

Call to function with no prototype 调用函数时没有函数的说明Cannot modify a const object 不允许修改常量对象

Case outside of switch 漏掉了case 语句

Case syntax error Case 语法错误

Code has no effect 代码不可述不可能执行到

Compound statement missing{ 分程序漏掉"{"

Conflicting type modifiers 不明确的类型说明符

Constant expression required 要求常量表达式

Constant out of range in comparison 在比较中常量超出范围Conversion may lose significant digits 转换时会丢失意义的数字

Conversion of near pointer not allowed 不允许转换近指针Could not find file ’xxx’ 找不到XXX 文件Declaration missing ; 说明缺少";

" Declaration syntax error 说明中出现语法错误

Default outside of switch Default 出现在switch 语句之外Define directive needs an identifier 定义编译预处理需要标识符

Division by zero 用零作除数

Do statement must have while Do-while 语句中缺少while 部分 Enum syntax error 枚举类型语法错误

Enumeration constant syntax error 枚举常数语法错误

Error directive :xxx 错误的编译预处理命令

Error writing output file 写输出文件错误

Expression syntax error 表达式语法错误

Extra parameter in call 调用时出现多余错误

File name too long 文件名太长

Function call missing ) 函数调用缺少右括号

Fuction definition out of place 函数定义位置错误

Fuction should return a value 函数必需返回一个值

Goto statement missing label Goto 语句没有标号

Hexadecimal or octal constant too large 16 进制或8 进制常数太大

Illeg al character ’x’ 非法字符x

Illegal initialization 非法的初始化

Illegal octal digit 非法的8 进制数字

Illegal pointer subtraction 非法的指针相减

Illegal structure operation 非法的结构体操作

Illegal use of floating point 非法的浮点运算

Illegal use of pointer 指针使用非法

Improper use of a typedefsymbol 类型定义符号使用不恰当

In-line assembly not allowed 不允许使用行间汇编Incompatible storage class 存储类别不相容

Incompatible type conversion 不相容的类型转换

Incorrect number format 错误的数据格式

Incorrect use of default Default 使用不当

Invalid indirection 无效的间接运算

Invalid pointer addition 指针相加无效

Irreducible expression tree 无法执行的表达式运算

Lvalue required 需要逻辑值0 或非0 值

Macro argument syntax error 宏参数语法错误

Macro expansion too long 宏的扩展以后太长

Mismatched number of parameters in definition 定义中参数个数不匹配

Misplaced break 此处不应出现break 语句 Misplaced continue 此处不应出现continue 语句

Misplaced decimal point 此处不应出现小数点

Misplaced elif directive 不应编译预处理elif

Misplaced else 此处不应出现else

Misplaced else directive 此处不应出现编译预处理else

Misplaced endif directive 此处不应出现编译预处理endif Must be addressable 必须是可以编址的

Must take address of memory location 必须存储定位的地址

No declaration for function ’xxx’ 没有函数xxx 的说明 No stack 缺少堆栈

No type information 没有类型信息

Non-portable pointer assignment 不可移动的指针(地址常数)赋值

Non-portable pointer comparison 不可移动的指针(地址常数)比较

Non-portable pointer conversion 不可移动的指针(地址常数)转换

Not a valid expression format type 不合法的表达式格式

Not an allowed type 不允许使用的类型

Numeric constant too large 数值常太大

Out of memory 内存不够用

Parameter ’xxx’ is never used 能数xxx 没有用到

Pointer required on left side of -> 符号->的左边必须是指针Possible use of ’xxx’ before definition 在定义之前就使用了xxx(警告)

Possibly incorrect assignment 赋值可能不正确

Redeclaration of ’xxx’ 重复定义了xxx Redefinition of ’xxx’ is not identical xxx 的两次定义不一致

Register allocation failure 寄存器定址失败

Repeat count needs an lvalue 重复计数需要逻辑值

Size of structure or array not known 结构体或数给大小不确定

Statement missing ; 语句后缺少";"

Structure or union syntax error 结构体或联合体语法错误Structure size too large 结构体尺寸太大

Sub scripting missing ] 下标缺少右方括号

Superfluous & with function or array 函数或数组中有多余的"&" Suspicious pointer conversion 可疑的指针转换

Symbol limit exceeded 符号超限

Too few parameters in call 函数调用时的实参少于函数的参数不

Too many default cases Default 太多(switch 语句中一个) Too many error or warning messages 错误或警告信息太多

Too many type in declaration 说明中类型太多

Too much auto memory in function 函数用到的局部存储太多

Too much global data defined in file 文件中全局数据太多Two consecutive dots 两个连续的句点

Type mismatch in parameter xxx 参数xxx 类型不匹配

Type mismatch in redeclaration of ’xxx’ xxx 重定义的类型不匹配

Unable to create output file ’xxx’ 无法建立输出文件

xxx Unable to open include file ’xxx’ 无法打开被包含的文件xxx Unable to open input file ’xxx’ 无法打开输入文件

xxx Undefined label ’xxx’ 没有定义的标号

xxx Undefined structure ’xxx’ 没有定义的结构

xxx Undefined symbol ’xxx’ 没有定义的符号

xxx Unexpected end of file in comment started on line xxx 从xxx 行开始的注解尚未结束文件不能结束

Unexpected end of file in conditional started on line xxx 从xxx 开始的条件语句尚未结束文件不能结束

Unknown assemble instruction 未知的汇编结构

Unknown option 未知的操作

Unknown preprocessor directive: ’xxx’ 不认识的预处理命令

xxx Unreachable code 无路可达的代码

Unterminated string or character constant 字符串缺少引号User break 用户强行中断了程序

Void functions may not return a value Void 类型的函数不应有返回值

Wrong number of arguments 调用函数的参数数目错

’xxx’ not an argument xxx 不是参数’xxx’ not part of structure xxx 不是结构体的一部分

xxx statement missing ( xxx 语句缺少左括号 xxx statement missing ) xxx 语句缺少右括号

xxx statement missing ; xxx 缺少分号

xxx’ declared but never used 说明了xxx 但没有使用

xxx’ is assigned a value which is never used 给xxx 赋了值但未用过

Zero length structure 结构体的长度为零

最常见的20 种VC++编译错误信息

1、 atal error C1010: unexpected end of file while looking for precompiled header directive。寻找预编译头文件路径时遇到了不该遇到的文件尾。(一般是没有#include "stdafx.h")

2、fatal error C1083: Cannot open include file: 'R…….h': No such file or directory 不能打开包含文件“R…….h”:没有这样

的文件或目录。

3、error C2011: 'C……': 'class' type redefinition 类“C……”重定义。

4、error C2018: unknown character '0xa3' 不认识的字符'0xa3'。(一般是汉字或中文标点符号)

5、error C2057: expected constant expression 希望是常量表达式。(一般出现在switch 语句的case 分支中)

6、error C2065: 'IDD_MYDIALOG' : undeclared identifier “IDD_MYDIALOG”:未声明过的标识符。

7、error C2082: redefinition of formal parameter 'bReset' 函数参数“bReset”在函数体中重定义。

8、error C2143: syntax error: missing ':' before '{' 句法错误:“{”前缺少“;”。

9、error C2146: syntax error : missing ';' before identifier 'dc' 句法错误:在“dc”前丢了“;”。

10、error C2196: case value '69' already used 值69 已经用过。(一般出现在switch 语句的case 分支中)

11、error C2509: 'OnTimer' : member function not declared in 'CHelloView' 成员函数“OnTimer”没有在“CHelloView”中声明。

12、error C2511: 'reset': overloaded member function 'void (int)' not found in 'B' 重载的函数“void reset(int)”在类“B”中找不到。

13、error C2555: 'B::f1': overriding virtual function differs from 'A::f1' only by return type or calling convention 类B 对类A 中同名函数f1 的重载仅根据返回值或调用约定上的区别。14、error C2660: 'SetTimer' : function does not take 2 parameters “SetTimer”函数不传递2 个参数。

15、warn ing C4035: 'f……': no return value “f……”的return 语句没有返回值。

16、warning C4553: '= =' : operator has no effect; did you intend '='? 没有效果的运算符“= =”;是否改为“=”?

17、warning C4700: local variable 'bReset' used without having been initialized 局部变量“bReset”没有初始化就使用。

18、error C4716: 'CMyApp::InitInstance' : must return a value “CMyApp::InitInstance”函数必须返回一个值。

19、LINK : fatal error LNK1168: cannot open Debug/P1.exe for writing 连接错误:不能打开P1.exe 文件,以改写内容。(一般是P1.Exe 还在运行,未关闭)

20 、error LNK2001: unresolved external symbol "public: virtual _ _thiscall C……::~C……(void)" 连接时发现没有实现的外部符号(变量、函数等)。

相关文档
最新文档