汉理工大学计理中院中心技交,0000计算机程序设计基础C语言主讲教师:王舜燕武汉理工大学Wuhan Universityof Technology
主讲教师:王舜燕
计算机程序设计基础C语言0编程风格与常见的几类编程错误0推80常见的程序代码错误常见的4类编程错误3种基本结构对照表武汉理工大学Wuhan Universityof Technology
计算机程序设计基础C语言 ⚫ 编程风格与常见的几类编程错误 ⚫ 常见的程序代码错误 ⚫ 常见的4类编程错误 ⚫ 3种基本结构对照表
编程风格与常见的几类编程错误编程风格ProgrammingStyle定义优雅的编程风格(Goodstyle)使得程序易读易懂,尤其是在程序调试中会起到事半功倍的作用。输入优雅的编程风格,可以包括以下几点:■有意义的变量名Meaningfulnames运算■语句间留白Whitespacetoseparatepartsofstatements.输出养成阶梯状编程格式的习惯Consistenthabitsregarding indentation
编程风格与常见的几类编程错误 编程风格 Programming Style 优雅的编程风格(Good style)使得程序易读易懂,尤 其是在程序调试中会起到事半功倍的作用。 优雅的编程风格,可以包括以下几点: ▪ 有意义的变量名 Meaningful names. ▪ 语句间留白 White space to separate parts of statements. ▪ 养成阶梯状编程格式的习惯 Consistent habits regarding indentation. 定义 输入 运算 输出
常见的程序代码错误Quotingproblems:引号不配对unmatchedquotes,mismatched quotes,incorrectquotesUnendingcomments注释没收尾Forgettingsemicolons()行尾没分号Unmatchedormismatchedbracesorparentheses括号不配对Control strings not matching the arguments in printfO, scanfO, and other similarfunctionsI/O函数中的格式与I/O项不一致Forgetting&inargumentstoscanf).scanfO中忘了&Lossoffractionsduetouseof intinsteadoffloat用int导致缺数Inconsistent lengths (e.g. double vs. float) betweenformats and arguments in scanfO) and printfO)I/O函数中格式类型不一致
常见的程序代码错误 Quoting problems: 引号不配对 unmatched quotes, mismatched quotes, incorrect quotes Unending comments 注释没收尾 Forgetting semicolons (;) 行尾没分号 Unmatched or mismatched braces or parentheses 括号不配对 Control strings not matching the arguments in printf(), scanf(), and other similar functions I/O函数中的格式与I/O项不一致 Forgetting & in arguments to scanf(). scanf()中忘了& Loss of fractions due to use of int instead of float. 用int导致缺数 Inconsistent lengths (e.g. double vs. float) between formats and arguments in scanf() and printf(). I/O函数中格式类型不一致
常见的4类编程错误语法错误(syntaxerror)由于1个语法错会导致许多错误信息,所以出错处往往在编译器所指出错行之前。运行时错误(runtimeerror)程序执行过程中试图执行非法操作逻辑错误(logicerror)由不正确算法导致的错误。未检测到的错误(uncheckederror)导致不正确结果的程序执行错。以下以一个例子来分别加以说明:
◼语法错误(syntax error) 由于1个语法错会导致许多错误信息,所以出错处往往在编译器所指出错行 之前。 ◼运行时错误(runtime error) 程序执行过程中试图执行非法操作 ◼逻辑错误(logic error) 由不正确算法导致的错误。 ◼未检测到的错误(unchecked error) 导致不正确结果的程序执行错。 以下以一个例子来分别加以说明: 常见的4类编程错误