Coe108 Week3
Coe108 Week3
Introduction to C Programming
C How to Program, 8/e
1
reserved.
2
3
4
2.4 Memory Concepts
Variable names such as integer1, integer2 and sum
memory.
6
7
2.4 Memory Concepts (Cont.)
8
2.4 Memory Concepts (Cont.)
9
10
2.4 Memory Concepts (Cont.)
11
2.4 Memory Concepts (Cont.)
12
13
2.4 Memory Concepts (Cont.)
14
2.5 Arithmetic in C
Most C programs perform calculations using the C
arithmetic operators (Fig. 2.9).
15
2.5 Arithmetic in C
18
19
2.5 Arithmetic in C (Cont.)
Arithmetic Expressions in Straight-Line Form
Arithmetic expressions in C must be written in straight-line
form to facilitate entering programs into the computer.
a divided by b
as a/b so that all operators and operands appear in a
straight line.
The algebraic notation
a
______
b
is generally not acceptable to compilers, although some
special-purpose software packages do support more
natural notation for complex mathematical expressions.
20
2.5 Arithmetic in C (Cont.)
21
2.5 Arithmetic in C (Cont.)
Rules of Operator Precedence
C applies the operators in arithmetic
expressions in a precise sequence determined
by the following rules of operator precedence,
which are generally the same as those in
algebra:
Operators in expressions contained within pairs of
parentheses are evaluated first. Parentheses are
cases of nested, or embedded, parentheses, such as
( ( a + b ) + c )
the operators in the innermost pair of parentheses are applied
first.
22
2.5 Arithmetic in C (Cont.)
Multiplication, division and remainder operations are
applied next. If an expression contains several
multiplication, division and remainder operations,
evaluation proceeds from left to right. Multiplication,
division and remainder are said to be on the same level
of precedence.
Addition and subtraction operations are evaluated next.
If an expression contains several addition and
subtraction operations, evaluation proceeds from left to
right. Addition and subtraction also have the same level
of precedence, which is lower than the precedence of
the multiplication, division and remainder operations.
The assignment operator (=) is evaluated last.
23
2.5 Arithmetic in C (Cont.)
right to left.
Figure 2.10 summarizes these rules of operator
24
25
2.5 Arithmetic in C (Cont.)
26
27
2.5 Arithmetic in C (Cont.)
28
2.6 Decision Making: Equality and
Relational Operators
Executable C statements either perform
actions (such as calculations or input or
output of data) or make decisions
soon see several examples of these).
We might make a decision in a program, for
29
2.6 Decision Making: Equality and
Relational Operators
31
2.6 Decision Making: Equality and
Relational Operators
Whether the body statement is executed or
not, after the if statement completes,
execution proceeds with the next statement
after the if statement.
Conditions in if statements are formed by
using the equality operators and relational
operators summarized in Fig. 2.12.
32
33
2.6 Decision Making: Equality and
Relational Operators
The relational operators all have the same
level of precedence and they associate left to
right.
The equality operators have a lower level of
precedence than the relational operators
and they also associate left to right.
In C, a condition may actually be any
expression that generates a zero (false) or
nonzero (true) value.
34
35
36
2.6 Decision Making: Equality and
Relational Operators
Figure 2.13 uses six if statements to
compare two numbers entered by the user.
37
38
39
40
2.6 Decision Making: Equality and
Relational Operators
The program uses scanf to input two
numbers.
Each conversion specifier has a
corresponding argument in which a value
will be stored.
The first %d converts a value to be stored in
the variable num1, and the second %d
converts a value to be stored in variable
num2.
41
42
43
2.6 Decision Making: Equality and
Relational Operators
Comparing Numbers
The if statement
if ( num1 == num2 ) {
printf( "%d is equal to %d\n", num1, num2 );
}
compares the values of variables num1 and num2 to test for
equality.
If the conditions are true in one or more of the if
statements, the corresponding body statement displays
an appropriate line of text.
Indenting the body of each if statement and placing
blank lines above and below each if statement
enhances program readability.
44
45
2.6 Decision Making: Equality and
Relational Operators
A left brace, {, begins the body of each if statement
A corresponding right brace, }, ends each if
body
Any number of statements can be placed in the body of
an if statement.
46
47
2.6 Decision Making: Equality and
Relational Operators
Figure 2.14 lists from highest to lowest the
precedence of the operators introduced in this
chapter.
Operators are shown top to bottom in
decreasing order of precedence.
The equals sign is also an operator.
All these operators, with the exception of the
assignment operator =, associate from left to
right.
The assignment operator (=) associates from
right to left.
48
49
50
2.6 Decision Making: Equality and
Relational Operators
51
52
2.7 Secure C Programming
CERT C Secure Coding Standard
Guidelines that help you avoid programming practices
that open systems to attacks
Avoid Single-Argument printfs
If you need to display a string that terminates with a
newline, use the puts function, which displays its string
argument followed by a newline character
For example
printf( "Welcome to C!\n" );
should be written as:
puts( "Welcome to C!" );
We did not include \n in the preceding string because
puts adds it automatically.
53
2.7 Secure C Programming (cont.)
54