c语言基础教程课件(英文版)ch(4)

上传人:tia****nde 文档编号:69989856 上传时间:2019-01-15 格式:PPT 页数:64 大小:1.94MB
返回 下载 相关 举报
c语言基础教程课件(英文版)ch(4)_第1页
第1页 / 共64页
c语言基础教程课件(英文版)ch(4)_第2页
第2页 / 共64页
c语言基础教程课件(英文版)ch(4)_第3页
第3页 / 共64页
c语言基础教程课件(英文版)ch(4)_第4页
第4页 / 共64页
c语言基础教程课件(英文版)ch(4)_第5页
第5页 / 共64页
点击查看更多>>
资源描述

《c语言基础教程课件(英文版)ch(4)》由会员分享,可在线阅读,更多相关《c语言基础教程课件(英文版)ch(4)(64页珍藏版)》请在金锄头文库上搜索。

1、A First Book of ANSI C Fourth Edition,Chapter 3 Processing and Interactive Input,A First Book of ANSI C, Fourth Edition,2,Objectives,Assignment Mathematical Library Functions Interactive Input Formatted Output,A First Book of ANSI C, Fourth Edition,3,Objectives (continued),Symbolic Constants Case St

2、udy: Interactive Input Common Programming and Compiler Errors,A First Book of ANSI C, Fourth Edition,4,Assignment,The general syntax for an assignment statement is variable = operand; The operand to the right of the assignment operator (=) can be a constant, a variable, or an expression The equal si

3、gn in C does not have the same meaning as an equal sign in algebra length=25; is read “length is assigned the value 25” Subsequent assignment statements can be used to change the value assigned to a variable length = 3.7; length = 6.28;,A First Book of ANSI C, Fourth Edition,5,Assignment (continued)

4、,The operand to the right of the equal sign in an assignment statement can be a variable or any valid C expression sum = 3 + 7; product = .05 * 14.6; The value of the expression to the right of = is computed first and then the calculated value is stored in the variable to the left of = Variables use

5、d in the expression to the right of the = must be initialized if the result is to make sense amount + 1892 = 1000 + 10 * 5 is invalid!,A First Book of ANSI C, Fourth Edition,6,Assignment (continued),If width was not initialized, the computer uses the value that happens to occupy that memory space pr

6、eviously (compiler would probably issue a warning),A First Book of ANSI C, Fourth Edition,7,Assignment (continued),= has the lowest precedence of all the binary and unary arithmetic operators introduced in Section 2.4 Multiple assignments are possible in the same statement a = b = c = 25; All = oper

7、ators have the same precedence Operator has right-to-left associativity c = 25; b = c; a = b;,A First Book of ANSI C, Fourth Edition,8,Implicit Type Conversions,Data type conversions take place across assignment operators double result; result = 4; /integer 4 is converted to 4.0 The automatic conver

8、sion across an assignment operator is called an implicit type conversion int answer; answer = 2.764; /2.764 is converted to 2 Here the implicit conversion is from a higher precision to a lower precision data type; the compiler will issue a warning,A First Book of ANSI C, Fourth Edition,9,Explicit Ty

9、pe Conversions (Casts),The operator used to force the conversion of a value to another type is the cast operator (dataType) expression where dataType is the desired data type of the expression following the cast Example: If sum is declared as double sum;, (int) sum is the integer value determined by

10、 truncating sums fractional part,A First Book of ANSI C, Fourth Edition,10,Assignment Variations,sum = sum + 10 is not an equationit is an expression that is evaluated in two major steps,A First Book of ANSI C, Fourth Edition,11,Assignment Variations (continued),A First Book of ANSI C, Fourth Editio

11、n,12,Assignment Variations (continued),A First Book of ANSI C, Fourth Edition,13,Assignment Variations (continued),Assignment expressions like sum = sum + 25 can be written using the following operators: += -= *= /= %= sum = sum + 10 can be written as sum += 10 price *= rate is equivalent to price =

12、 price * rate price *= rate + 1 is equivalent to price = price * (rate + 1),A First Book of ANSI C, Fourth Edition,14,Accumulating,The first statement initializes sum to 0 This removes any previously stored value in sum that would invalidate the final total A previously stored number, if it has not

13、been initialized to a specific and known value, is frequently called a garbage value,A First Book of ANSI C, Fourth Edition,15,Accumulating (continued),A First Book of ANSI C, Fourth Edition,16,Accumulating (continued),A First Book of ANSI C, Fourth Edition,17,Counting,A counting statement is very s

14、imilar to the accumulating statement variable = variable + fixedNumber; Examples: i = i + 1; and m = m + 2; Increment operator (+): variable = variable + 1 can be replaced by variable+ or +variable,A First Book of ANSI C, Fourth Edition,18,Counting (continued),A First Book of ANSI C, Fourth Edition,

15、19,Counting (continued),A First Book of ANSI C, Fourth Edition,20,Counting (continued),When the + operator appears before a variable, it is called a prefix increment operator; when it appears after a variable, it is called postfix increment operator k = +n; is equivalent to n = n + 1; / increment n

16、first k = n; / assign ns value to k k = n+; is equivalent to k = n; / assign ns value to k n = n + 1; / and then increment n,A First Book of ANSI C, Fourth Edition,21,Counting (continued),Prefix decrement operator: the expression k = -n first decrements the value of n by 1 before assigning the value of n to k Postfix decrement operator: the expression k = n- first assigns the current value of n to n and then reduces the value of n by 1,A First Book of ANSI C, Fourth Edition,2

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号