C语言基础教程英文版

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
• The equal sign 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
A First Book of ANSI C
Fourth Edition
Chapter 3 Processing and Interactive Input
Objectives
• Assignment • Mathematical Library Functions • Interactive Input • Formatted Output
A First Book of ANSI C, Fourth Edition
3syntax 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
A First Book of ANSI C, Fourth Edition
8
Explicit Type Conversions (Casts)
• The operator used to force the conversion of a value to another type is the cast operator
A First Book of ANSI C, Fourth Edition
6
Assignment (continued)
• = has the lowest precedence of all the binary and unary arithmetic operators introduced in Section 2.4
• Variables used 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!
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 =
• The automatic conversion 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
5
Assignment (continued)
If width was not initialized, the computer uses the value that happens to occupy that memory space previously (compiler would probably issue a warning)
• Multiple assignments are possible in the same statement
a = b = c = 25;
• All = operators have the same precedence • Operator has right-to-left associativity
A First Book of ANSI C, Fourth Edition
2
Objectives (continued)
• Symbolic Constants • Case Study: Interactive Input • Common Programming and Compiler Errors
(dataType) expression
• where dataType is the desired data type of the expression following the cast
length = 3.7; length = 6.28;
A First Book of ANSI C, Fourth Edition
4
Assignment (continued)
• The operand to the right of the equal sign in an assignment statement can be a variable or any valid C expression
c = 25; b = c; a = b;
A First Book of ANSI C, Fourth Edition
7
Implicit Type Conversions
• Data type conversions take place across assignment operators
double result; result = 4; //integer 4 is converted to 4.0
相关文档
最新文档