Professional Documents
Culture Documents
Overview of C
Part II
J. H. Wang (), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering
Arithmetic Expressions
2-2
In m % n, the operation is undefined when n is zero and varies from one implementation to another if n is negative.
Copyright 2004 Pearson Addison-Wesley. All rights reserved. 2-3
2-4
2-5
2-6
2-7
2-8
2-10
Arithmetic Expressions -- Mixed-Type Assignment Statement (Cont) The expression is evaluated before the assignment is made, and the type of the variable being assigned has no effect whatsoever on the expression value. E.g. If m and n are type int and p, x, and y are type double:
m = 3; n = 2; p = 2.0; x = m / p; y = m / n;
2-11
Assignment of a type double expression to a type int variable causes the fractional part of the expression to be lost since it cannot be represented in a type int variable. E.g.
x = 9 * 0.5; n = 9 * 0.5;
2-12
2-13
2-14
binary operator
an operator with two operands
2-15
Arithmetic Expressions -- Expressions with Multiple Operators (Cont) Rules for Evaluating Expressions
a. Parentheses rule: All expressions in parentheses must be evaluated separately. Nested parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated first. b. Operator precedence rule: Operators in the same expression are evaluated in the following order:
unary +, - first *, /, % next binary +, - last
2-16
Arithmetic Expressions -- Expressions with Multiple Operators (Cont) c. Associativity rule: Unary operators in the same subexpression and at the same precedence level (such as + and -) are evaluated right to left (right associativity). Binary operators in the same subexpression and at the same precedence level (such as + and -) are evaluated left to right (left associativity).
2-17
2-18
2-19
2-20
2-21
Figure 2.10 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);
2-22
2-23
2-24
ANALYSIS
get the count of each type of coin from a customer. determine the total value of the coins in cents do an integer division using 100 as the divisor to get the dollar value remainder of this division will be the leftover change
Copyright 2004 Pearson Addison-Wesley. All rights reserved. 2-26
Problem Outputs
int dollars /* value in dollars */ int change /* leftover change */
2-28
Step 4 Refinement
4.1 dollars is the integer quotient of total_cents and 100. 4.2 change is the integer remainder of total_cents and 100.
2-29
2-30
2-31
E.g.
printf("Results: %3d meters = %4d ft. %2d in.\n", meters, feet, inches); Results: 21 meters = 68 ft. 11 in.
2-32
Expands the field width if it is too small for the integer value displayed.
Copyright 2004 Pearson Addison-Wesley. All rights reserved. 2-33
2-34
%n.mf
n is a number representing the total field width m is the desired number of decimal places
2-35
It is legal to omit the total field width in the format string placeholder, such as %.mf, which will be printed with no leading blanks.
2-36
2-37
2-38
batch mode
a mode of program execution in which the program scans its data from a previously prepared data file
2-39
Follow the call to scanf with the echo statement to display the value just stored as a record of the data manipulated by the program. Output Redirection
metric >myoutput
Interacting with the running program as above will be difficult because all program output, including any prompting messages, will be sent to the output file.
metric <mydata >myoutput
2-40
2-41
Interactive Mode, Batch Mode, and Data Files -- Program-Controlled Input and Output Files
file pointer
store the information necessary to permit access to a file FILE *inp, /* pointer to input file */ *outp; /* pointer to output file */
Interactive Mode, Batch Mode, and Data Files -- Program-Controlled Input and Output Files (Cont)
File closing
fclose(inp); fclose(outp);
2-43
2-44
2-45
A compiler listing is a listing created by the compiler during program translation that shows each line of the source program (preceded by a line number) and any syntax errors detected by the compiler.
E.g. Fig 2.15
Copyright 2004 Pearson Addison-Wesley. All rights reserved. 2-46
2-47
One mistake of the programmer leads to the generation of multiple error messages.
miles
2-48
Correct the errors in the declaration part of a program first, then recompile the program before you attempt to fix other errors.
Copyright 2004 Pearson Addison-Wesley. All rights reserved. 2-49
2-50
2-51
2-52
2-53
Expected result:
Hello BMC, let's check your coins' value in 2003.
Actual result:
Hello BM, let's check your coins' value in 2003.
2-54
2-55
Figure 2.18 A Program That Produces Incorrect Results Due to & Omission
2-56
Logic errors may not cause run-time errors and do not display error messages. To prevent logic errors, carefully desk check the algorithm and the program.
2-57
Chapter Review
Every C program has preprocessor directives and a main function. The main function contains variable declarations and executable statements. Variable names must begin with a letter or an underscore (the latter not recommended) and consist of letters, digits, and underscore symbols. A reserved word cannot be used as an identifier.
Copyright 2004 Pearson Addison-Wesley. All rights reserved. 2-58
2-59
New Constructs
2-60
2-61