《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-11 编程风格与常见错误

汉理工大学计理中院中心技交,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类编程错误

1:语法错误syntaxerror//换算后输出y的原值main()(intx,Y,t;printf("x-");scanf("%d",&x);printf("y=");scanf("%d",&y);t=x/y;x=x/t;printf("x=%d",x);getch() ;c2146:syntax error:missing";'before identifier'printfDEMO.C(3)errorwarning c40i3:'printf'undefined; assuming extern returning intDEMO.C(3):C200i:newiine in constantDEMO.C(3)errorc2065:"d::undeclared identifierDEMO.C(3)errorc2296:g:iilegal,left operand has type'char [11]'DEMO.C(3)errorDEMO.C(3)C2143::missing")before‘string'errorsyntax errorDEMO.C(4)warningC4013:'scanf'undefined; assuming extern returning int:DEMO.C(8)C4013:'getch'undefined;assuming extern returning int.warning
1:语法错误(syntax error) DEMO.C(3) : error C2146: syntax error : missing ';' before identifier 'printf' DEMO.C(3) : warning C4013: 'printf' undefined; assuming extern returning int DEMO.C(3) : error C2001: newline in constant DEMO.C(3) : error C2065: 'd' : undeclared identifier DEMO.C(3) : error C2296: '%' : illegal, left operand has type 'char [11]' DEMO.C(3) : error C2143: syntax error : missing ')' before 'string' DEMO.C(4) : warning C4013: 'scanf' undefined; assuming extern returning int DEMO.C(8) : warning C4013: 'getch' undefined; assuming extern returning int //换算后输出y的原值 main(){ int x,y,t printf("x=);scanf("%d",&x); printf("y=");scanf("%d",&y); t=x/y; x=x/t; printf("x=%d",x); getch(); } //换算后输出y的原值 main(){ int x,y,t; printf("x=");scanf("%d",&x); printf("y=");scanf("%d",&y); t=x/y; x=x/t; printf("x=%d",x); getch(); }

2:运行时错误runtime error//换算后输出的原值main()(int x,Y,t;printf("x=");scanf("%d",&x);printf("y=");scanf("%d",&y);t=x/y;X=x/t;printf("x=%d",x);getch();1XMicrosoftVisualC++X=6←Unhandled exception in demo:exe: 0xc0000094: Integer Divide byy=2LZero.确定X=2Ly=6L
2:运行时错误(runtime error) x=2 y=6 //换算后输出y的原值 main(){ int x,y,t; printf("x=");scanf("%d",&x); printf("y=");scanf("%d",&y); t=x/y; x=x/t; printf("x=%d",x); getch(); } x=6 y=2

3:逻辑错误(logic error)//换算后输出y的原值main()(int x,Y,t;printf("x=");scanf("%d",&x);printf("y=");scanf("%d",&y);t=x/y;x=y/t;printf("x=%d",x);getch();1X=6Ly=2LX=0
3:逻辑错误(logic error) x=6 y=2 x=0 //换算后输出y的原值 main(){ int x,y,t; printf("x=");scanf("%d",&x); printf("y=");scanf("%d",&y); t=x/y; x=y/t; printf("x=%d",x); getch(); }

.未检测到的错误uncheckederror//换算后输出y的原值main()(int x,Y,t;printf("x=");scanf("%d",x);printf("y=");scanf("%d",&y);t=x/y;x=y/t;printf("x=%d",x);getch();AX=6←MicrosoftVisualC+Unhandled exception in demo.exe:0OxC0000005:Access Violation确定非法访问
4:未检测到的错误(unchecked error) x=6 //换算后输出y的原值 main(){ int x,y,t; printf("x=");scanf("%d",x); printf("y=");scanf("%d",&y); t=x/y; x=y/t; printf("x=%d",x); getch(); } 非法访问

Well began, half done.相信自己、做好自己勤动脑、多动手百“练”成钢
Well began, half done. 相信自己、做好自己 勤动脑、多动手 百“练”成钢
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-8 标识符和关键字.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-10 IO函数值和注释语句.ppt
- 《计算机程序设计基础》课程授课教案(C语言)第8章 数据文件编程方法.pdf
- 《计算机程序设计基础》课程授课教案(C语言)第7章 结构体与共同体.pdf
- 《计算机程序设计基础》课程授课教案(C语言)第6章 指针进阶与内存空间管理.pdf
- 《计算机程序设计基础》课程授课教案(C语言)第5章 模块化编程.pdf
- 《计算机程序设计基础》课程授课教案(C语言)第4章 数组和指针.pdf
- 《计算机程序设计基础》课程授课教案(C语言)第3章 控制结构.pdf
- 《计算机程序设计基础》课程授课教案(C语言)第2章 基本数据类型和运算符.pdf
- 《计算机程序设计基础》课程授课教案(C语言)第1章 C语言概述.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 15 - Data Structures.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 14 - Files.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 13 - More functions.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 12 - Basics of Functions.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 11 - Strings.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 10 - Basics of Pointers.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 9 - Arrays.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 3 - Hardware and Software.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 8 - Looping.pdf
- 英格兰萨里大学:《C语言》课程教学资源(讲义)Lecture 7 - Making Decisions.pdf
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-9 输入输出函数.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-7 实例求圆柱体底面积和体积.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-5 第1个C程序解析.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-6 预处理指令#define#include.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-4 算法的表示.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-1 C语言的概述.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-2 结构化程序设计方法.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第1章 C语言概述 1-3 算法的概念和特点.ppt
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-7 自增和自减运算符.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-8 逗号运算符和逗号表达式.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-10 混合运算.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-9 其他运算符.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-3 变量.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-4 指针变量.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-6 赋值运算符和赋值表达式.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-1 基本数据类型.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-2 常量.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第2章 基本数据类型和运算符 2-5 算术运算符和算术表达式.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第3章 控制结构 3-4 程序中的选择结构-if语句的嵌套形式.pptx
- 《计算机程序设计基础》课程PPT教学课件(C语言)第3章 控制结构 3-3 程序中的选择结构-if语句的简单形式.pptx