(完整word版)C语言程序编译常见错误对照
- 格式:doc
- 大小:20.61 KB
- 文档页数:6
c语言常见问题及解决方法
一、C语言常见问题及解决方法
1、程序编译错误,原因及解决方法
(1)语法错误:检查程序中出现的缩进、分号、圆括号、大括号的位置,以及程序中变量的定义,保证程序的语法正确。
(2)类型错误:检查程序中关系运算符两边操作数的类型是否匹配,以及变量的使用是否正确,保证每一步运算的类型正确。
(3)变量未声明:检查变量在程序中是否已经声明,声明后才能正确使用。
2、程序运行错误,原因及解决方法
(1)程序中存在逻辑错误:检查程序中的流程是否按设计要求正确,以及程序输出结果是否正确。
(2)程序中存在数据错误:检查程序中的数据是否正确,数据输入、输出以及运算结果都要求正确。
(3)程序运行错误:检查程序中的函数调用是否正确,注意函数的参数和返回值的类型要求。
3、程序编译成功却无法执行,原因及解决方法
这可能是程序出现了语法错误,编译器无法判断,所以编译成功,但是在执行时系统无法识别出程序的命令。
可以通过重新编写程序,查找错误语句的方式查找程序错误,并根据提示修改程序,以解决此问题。
C语言程序设计中的常见错误和解决在C语言程序设计中,很多初学者经常会遇到一些常见的错误。
这些错误可能是语法错误、逻辑错误或者是其他一些常见问题导致的。
本文将介绍一些经常出现的C语言程序设计错误,并提供相应的解决方法,帮助读者更好地理解和掌握C语言编程。
一、语法错误1. 括号未匹配括号未匹配是C语言中常见的语法错误之一。
在编写代码时,经常会使用到括号,如if语句、for循环等。
如果不小心忘记关闭某个括号,就会导致括号未匹配的错误。
解决方法:仔细检查代码中的括号是否匹配,并确保每个左括号都有相应的右括号。
2. 分号丢失分号丢失是C语言中另一个常见的语法错误。
在C语言中,分号用于分隔语句,如果遗漏了分号,编译器就无法识别出语句的结束。
解决方法:检查每一行语句的末尾,确保每个语句都以分号结尾。
3. 变量未声明在使用变量之前,必须先声明该变量。
如果没有先声明变量就直接使用,编译器将无法识别该变量,从而报错。
解决方法:在使用变量之前,先进行变量声明。
二、逻辑错误1. 循环控制条件错误在编写循环语句时,控制条件的错误是常见的逻辑错误。
如果循环的控制条件错误,循环可能无法正常结束或者根本无法执行。
解决方法:仔细检查循环的控制条件,确保条件符合预期,并在循环体内更新循环变量。
2. 数组越界访问在C语言中,数组的索引从0开始,通过索引访问数组元素时,如果索引超出了数组的范围,就会导致数组越界访问的错误。
解决方法:确保数组索引在正确的范围内,避免越界访问。
如果需要遍历数组,可以使用循环来控制数组的访问范围。
三、其他常见问题及解决方法1. 变量类型不匹配在赋值操作或者表达式计算时,如果不同类型的变量之间进行操作,则会导致变量类型不匹配的错误。
解决方法:确保操作的变量类型相同或者进行强制类型转换,以保证变量类型的匹配。
2. 内存泄漏在C语言中,手动分配内存的操作是常见的。
如果分配了内存空间,但在使用完毕后未及时释放,就会导致内存泄漏。
C语言错误代码及错误信息错误释义error 1: Out of memory 内存溢出error 2: Identifier expected 缺标识符error 3: Unknown identifier 未定义的标识符error 4: Duplicate identifier 重复定义的标识符error 5: Syntax error 语法错误error 6: Error in real constant 实型常量错误error 7: Error in integer constant 整型常量错误error 8: String constant exceeds line 字符串常量超过一行error 10: Unexpected end of file 文件非正常结束error 11: Line too long 行太长error 12: Type identifier expected 未定义的类型标识符error 13: Too many open files 打开文件太多error 14: Invalid file name 无效的文件名error 15: File not found 文件未找到error 16: Disk full 磁盘满error 17: Invalid compiler directive 无效的编译命令error 18: Too many files 文件太多error 19: Undefined type in pointer def 指针定义中未定义类型error 20: Variable identifier expected 缺变量标识符error 21: Error in type 类型错误error 22: Structure too large 结构类型太长error 23: Set base type out of range 集合基类型越界error 24: File components may not be files or objectsfile分量不能是文件或对象error 25: Invalid string length 无效的字符串长度error 26: Type mismatch 类型不匹配error 27:error 27:Invalid subrange base type 无效的子界基类型error 28:Lower bound greater than upper bound 下界超过上界error 29:Ordinal type expected 缺有序类型error 30:Integer constant expected 缺整型常量error 31:Constant expected 缺常量error 32:Integer or real constant expected 缺整型或实型常量error 33:Pointer Type identifier expected 缺指针类型标识符error 34:Invalid function result type 无效的函数结果类型error 35:Label identifier expected 缺标号标识符error 36:BEGIN expected 缺BEGINerror 37:END expected 缺ENDerror 38:Integer expression expected 缺整型表达式error 39:Ordinal expression expected 缺有序类型表达式error 40:Boolean expression expected 缺布尔表达式error 41:Operand types do not match 操作数类型不匹配error 42:Error in expression 表达式错误error 43:Illegal assignment 非法赋值error 44:Field identifier expected 缺域标识符error 45:Object file too large 目标文件太大error 46:Undefined external 未定义的外部过程与函数error 47:Invalid object file record 无效的OBJ文件格式error 48:Code segment too large 代码段太长error 49:Data segment too large 数据段太长error 50:DO expected 缺DOerror 51:Invalid PUBLIC definition 无效的PUBLIC定义error 52:Invalid EXTRN definition 无效的EXTRN定义error 53: Too many EXTRN definitions 太多的EXTRN定义error 54:OF expected 缺OFerror 55:INTERFACE expected 缺INTERFACEerror 56:Invalid relocatable reference 无效的可重定位引用error 57:THEN expected 缺THENerror 58:TO or DOWNTO expected 缺TO或DOWNTO error 59:Undefined forward 提前引用未经定义的说明error 61:Invalid typecast 无效的类型转换error 62:Division by zero 被零除error 63:Invalid file type 无效的文件类型error 64:Cannot read or write variables of this type 不能读写此类型变量error 65:Pointer variable expected 缺指针类型变量error 66:String variable expected 缺字符串变量error 67:String expression expected 缺字符串表达式error 68:Circular unit reference 单元UNIT部件循环引用error 69:Unit name mismatch 单元名不匹配error 70:Unit version mismatch 单元版本不匹配error 71:Internal stack overflow 内部堆栈溢出error 72:Unit file format error 单元文件格式错误error 73:IMPLEMENTATION expected 缺IMPLEMENTATIONerror 74:Constant and case types do not match 常量和CASE类型不匹配error 75:Record or object variable expected 缺记录或对象变量error 76:Constant out of range 常量越界error 77:File variable expected 缺文件变量error 78:Pointer expression expected 缺指针表达式error 79:Integer or real expression expected 缺整型或实型表达式error 80:Label not within current block 标号不在当前块内error 81:Label already defined 标号已定义error 82:Undefined label in preceding statement part 在前面未定义标号error 83:Invalid @ argument 无效的@参数error 84:UNIT expected 缺UNITerror 85: ";" expected 缺“;”error 86:":" expected 缺“:”error 87:"," expected 缺“,”error 88:"(" expected 缺“(”error 89:")" expected 缺“)”error 90:"=" expected 缺“=”error 91:":=" expected 缺“:=”error 92:"[" or "(." Expected 缺“[”或“(.”error 93: "]" or ".)" expected 缺“]”或“.)”error 94:"." expected 缺“.”error 95: ".." expected 缺“..”error 96:Too many variables 变量太多error 97:Invalid FOR control variable 无效的FOR循环控制变量error 98:Integer variable expected 缺整型变量error 99:Files and procedure types are not allowed here 该处不允许文件和过程类型error 100:String length mismatch 字符串长度不匹配error 101:Invalid ordering of fields 无效域顺序error 102:String constant expected 缺字符串常量error 103:Integer or real variable expected 缺整型或实型变量error 104:Ordinal variable expected 缺有序类型变量error 105:INLINE error INLINE错误error 106:Character expression expected 缺字符表达式error 107:Too many relocation items 重定位项太多error 108:Overflow in arithmetic operation 算术运算溢出error 112:CASE constant out of range CASE常量越界error 113:Error in statement 表达式错误error 114:Cannot call an interrupt procedure 不能调用中断过程error 116:Must be in 8087 mode to compile this 必须在8087模式编译error 117:Target address not found 找不到目标地址error 118:Include files are not allowed here 该处不允许INCLUDE文件error 119:No inherited methods are accessible here 该处继承方法不可访问error 121:Invalid qualifier 无效的限定符error 122:Invalid variable reference 无效的变量引用error 123:Too many symbols 符号太多error 124:Statement part too large 语句体太长error 126:Files must be var parameters 文件必须是变量形参error 127:Too many conditional symbols 条件符号太多error 128:Misplaced conditional directive 条件指令错位error 129:ENDIF directive missing 缺ENDIF指令error 130:Error in initial conditional defines 初始条件定义错误error 131:Header does not match previous definition 和前面定义的过程或函数不匹配error 133:Cannot evaluate this expression 不能计算该表达式error 134:Expression incorrectly terminated 表达式错误结束error 135:Invalid format specifier 无效格式说明符error 136:Invalid indirect reference 无效的间接引用error 137:Structured variables are not allowed here 该处不允许结构变量error 138:Cannot evaluate without System unit 没有System单元不能计算error 139:Cannot access this symbol 不能存取符号error 140:Invalid floating point operation 无效的符号运算error 141:Cannot compile overlays to memory 不能编译覆盖模块至内存error 142:Pointer or procedural variable expected 缺指针或过程变量error 143:Invalid procedure or function reference 无效的过程或函数调用error 144:Cannot overlay this unit 不能覆盖该单元error 146:File access denied 不允许文件访问error 147:Object type expected 缺对象类型error 148:Local object types are not allowed 不允许局部对象类型error 149:VIRTUAL expected 缺VIRTUALerror 150: Method identifier expected 缺方法标识符error 151:Virtual constructors are not allowed 不允许虚构造函数error 152:Constructor identifier expected 缺构造函数标识符error 153:Destructor identifier expected 缺析构函数标识符error 154:Fail only allowed within constructors 只能在构造函数内使用Fail标准过程error 155:Invalid combination of opcode and operands 操作数与操作符无效组合error 156:Memory reference expected 缺内存引用指针error 157:Cannot add or subtract relocatable symbols 不能加减可重定位符号error 158:Invalid register combination 无效寄存器组合error 159:286/287 instructions are not enabled 未激活286/287指令error 160:Invalid symbol reference 无效符号指针error 161:Code generation error 代码生成错误error 162:ASM expected 缺ASMerror 166:Procedure or function identifier expected 缺过程或函数标识符error 167:Cannot export this symbol 不能输出该符号error 168:Duplicate export name 外部文件名重复error 169:Executable file header toerror 170:Too many segments 段太多fatal error C1004: unexpected end of file found 未找到文件末尾(可能是括号匹配问题)fatal error C1021: invalid preprocessor command '1nclude' 无效的编译预处理命令'1nclude' fatal error C1083: Cannot open include file: 'stdi.h': No such file or directory 不能打开头文件'stdi.h',文件或文件夹不存在 error C2101: '&' on constant 不能计算常量的地址error C2059: syntax error : 'while' 在'while'附近,存在语法错误error C2061: syntax error : identifier 'x' 标识符x的附近,存在语法错误 error C2065: 'i' : undeclared identifier 变量i未定义error C2078: too many initializers 数组/结构等变量初始化时的数据太多error C2087: '<Unknown>' : missing subscript 丢失数组下标error C2106: '=' : left operand must be l-value '='的左侧应当是左值,即不能是常量 error C2115: '=' : incompatible types '='两侧的类型不兼容error C2133: 'a' : unknown size a(可能是数组名)的大小不确定。
C语⾔编程中常见的五种错误及对应解决⽅案⽬录1. 未初始化的变量2. 数组越界3. 字符串溢出4. 重复释放内存5. 使⽤⽆效的⽂件指针前⾔:C 语⾔有时名声不太好,因为它不像近期的编程语⾔(⽐如 Rust)那样具有内存安全性。
但是通过额外的代码,⼀些最常见和严重的 C 语⾔错误是可以避免的。
即使是最好的程序员也⽆法完全避免错误。
这些错误可能会引⼊安全漏洞、导致程序崩溃或产⽣意外操作,具体影响要取决于程序的运⾏逻辑。
下⽂讲解了可能影响应⽤程序的五个错误以及避免它们的⽅法:1. 未初始化的变量程序启动时,系统会为其分配⼀块内存以供存储数据。
这意味着程序启动时,变量将获得内存中的⼀个随机值。
有些编程环境会在程序启动时特意将内存“清零”,因此每个变量都得以有初始的零值。
程序中的变量都以零值作为初始值,听上去是很不错的。
但是在 C 编程规范中,系统并不会初始化变量。
看⼀下这个使⽤了若⼲变量和两个数组的⽰例程序:#include <stdio.h>#include <stdlib.h>intmain(){int i, j, k;int numbers[5];int *array;puts("These variables are not initialized:");printf(" i = %d\n", i);printf(" j = %d\n", j);printf(" k = %d\n", k);puts("This array is not initialized:");for (i = 0; i < 5; i++) {printf(" numbers[%d] = %d\n", i, numbers[i]);}puts("malloc an array ...");array = malloc(sizeof(int) * 5);if (array) {puts("This malloc'ed array is not initialized:");for (i = 0; i < 5; i++) {printf(" array[%d] = %d\n", i, array[i]);}free(array);}/* done */puts("Ok");return 0;}这个程序不会初始化变量,所以变量以系统内存中的随机值作为初始值。
C51语言编写常见错误一、循环1)、在进行while,do...while循环时要对while括号里面的变量赋初值,否则可能出现的不是自己想要的结果。
2)、在写for循环语句中,特别要注意括号里面的变量要一致(例如:for(i=0;i<8;j++)。
)错误。
这个错误不好改,写程序时一定要注意这一点。
3)、在写for循环时,如果想让变量的值一次性增加不是一而是大于一,这时要注意最后变量的变化要写成表达式例如:i=i+10;不要写成i+10,这个不起作用。
4)、在写循环时,对某些变量赋初值时,注意赋初值所放的位置,是放在循环里面还是外面。
不同的地方可能得到的结果不同。
要注意二、结构体、链表1)、在创建链表时,最好对首个结点赋值,否则在编译时可能会出现错误。
(如:后面会显示屯屯屯屯屯屯?)。
三、条件语句1)、在运用if条件语句时,括号里面的等于不要写成赋值号(例如:if(a==1)不要写成if(a=1))。
也就是说在if语句的条件中不要出现赋值号。
四、函数1)、在函数进行参数传递时类型要一致,且只能是实参传递到形参,不能反过来传递。
传递的实参必须是常量(在使用的时候)。
2)、在写主函数时,定义一些变量、数组或指针时,最好在主函数的最前面就定义,否则可能会出现错误。
3)、在写输入函数scanf时,后面的变量前面要加取地址符(例如:scanf(“%d”,&a),不要省略&)。
4)、在写字符串复制函数(strcpy)时切忌写错,把strcpy写成stycpy。
5)、不能直接把字符串赋给一个变量,如果要把一个字符串赋给一个变量,需要用到字符串复制函数strcpy,这个函数包含在“string.h”头文件里面,用时需要将此头文件写在程序的开头。
Strcpy(char *a,char b ),将字符串b复制到字符串a里面,注意字符串a的长度要大于等于字符串b,返回值为字符串a。
五、运算1)、在计算两个数的成积时,不要省略乘号*,例如:s=-b/(2*a)正确;s=-b/2a 错误;2)、在进行除法运算时,要注意除法与取整的区别。
常见C语言错误提示信息Ambiguous operators need parentheses 不明确的运算需要用括号括起Ambiguous symbol ''xxx''不明确的符号Argument list syntax error参数表语法错误Array bounds missing丢失数组界限符Array size too large数组尺寸太大Bad character in parameters参数中有不适当的字符Bad file name format in include directive 包含命令中文件名格式不正确Bad ifdef directive syntax编译预处理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 errorCase 语法错误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 switchDefault 出现在switch语句之外Define directive needs an identifier定义编译预处理需要标识符Division by zero 用零作除数Do statement must have whileDo-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 labelGoto语句没有标号Hexadecimal or octal constant too large 16进制或8进制常数太大Illegal character ''x''非法字符xIllegal 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 defaultDefault使用不当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不应编译预处理elifMisplaced else 此处不应出现elseMisplaced 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''重复定义了xxxRedefinition 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 casesDefault太多(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''无法建立输出文件xxxUnable to open include file ''xxx''无法打开被包含的文件xxxUnable to open input file ''xxx''无法打开输入文件xxxUndefined label ''xxx''没有定义的标号xxxUndefined structure ''xxx''没有定义的结构xxxUndefined symbol ''xxx''没有定义的符号xxxUnexpected 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''不认识的预处理命令xxxUnreachable code无路可达的代码Unterminated string or character constant字符串缺少引号User break用户强行中断了程序Void functions may not return a valueVoid类型的函数不应有返回值Wrong number of arguments调用函数的参数数目错''xxx'' not an argumentxxx不是参数''xxx'' not part of structurexxx不是结构体的一部分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结构体的长度为零。
Turbo C编译、连接和运行时的常见错误&C语言编程风格一、编译时的常见错误1. 数据类型错误。
此类错误是初学者编程时的常见现象, 下面是一些要引起注意的错误:(1) 所有变量和常量必须要加以说明。
(2) 变量只能赋给相同类型的数据。
(3) 对scanf()语句, 用户可能输入错误类型的数据项, 这将导致运行时出错, 并报出错信息。
为避免这样的错误出现, 你就提示用户输入正确类型的数据。
(4) 在执行算术运算时要注意:a. 根据语法规则书写双精度数字。
要写0.5, 而不是写.5; 要写1.0,而不是1。
尽管C语言会自动地把整型转换成双精度型, 但书写双精度型是个好习惯。
让C语言为你做强行转换这是一种效率不高的程序设计风格。
这有可能导致转换产生错误。
b. 不要用0除。
这是一个灾难性的错误, 它会导致程序失败, 不管C语言的什么版本, 都是如此, 执行除法运算要特别小心。
c. 确保所有的双精度数(包括那些程序输入用的双精度数) 是在实数范围之内。
d. 所有整数必须在整数允许的范围内。
这适用于所有计算结果, 包括中间结果。
2. 将函数后面的";"忘掉。
此时错误提示色棒将停在该语句下的一行, 并显示:Statement missing ; in function <函数名>3. 给宏指令如#include, #define等语句尾加了";"号。
4. "{"和"}"、"("和")"、"/*"和"*/"不匹配。
引时色棒将位于错误所在的行, 并提示出有关丢掉括号的信息。
5. 没有用#include指令说明头文件, 错误信息提示有关该函数所使用的参数未定义。
6. 使用了Turbo C保留关键字作为标识符, 此时将提示定义了太多数据类型。