0% found this document useful (0 votes)
6 views

Coe108 Week3

The document discusses memory concepts in C programming. It explains that every variable has a name, type, and value, and that values are stored in memory locations associated with the variable names. It provides examples to illustrate how user input gets stored in memory locations for variables. It also discusses arithmetic operations in C and how expressions are evaluated based on operator precedence rules. Finally, it introduces conditional statements using relational operators to make decisions based on evaluating conditions.

Uploaded by

batuhanguzel35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Coe108 Week3

The document discusses memory concepts in C programming. It explains that every variable has a name, type, and value, and that values are stored in memory locations associated with the variable names. It provides examples to illustrate how user input gets stored in memory locations for variables. It also discusses arithmetic operations in C and how expressions are evaluated based on operator precedence rules. Finally, it introduces conditional statements using relational operators to make decisions based on evaluating conditions.

Uploaded by

batuhanguzel35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Chapter 2

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.

Every variable has a name, a type and a value.

In the addition program of Fig. 2.5, when the statement

scanf( "%d", &integer1 ); // read an integer

is executed, the value entered by the user is placed into a


memory location to which the name integer1 has been
assigned.
5
2.4 Memory Concepts (Cont.)

Suppose the user enters the number 45 as


the value for integer1.

The computer will place 45 into location


integer1 as shown in Fig. 2.6.

6
7
2.4 Memory Concepts (Cont.)

Whenever a value is placed in a memory location, the


value replaces the previous value in that location; thus,
this process is said to be destructive.

When the statement

scanf( "%d", &integer2 ); // read an integer

executes, suppose the user enters the value 72.

8
2.4 Memory Concepts (Cont.)

This value is placed into location integer2,


and memory appears as in Fig. 2.7.

These locations are not necessarily adjacent


in memory.

9
10
2.4 Memory Concepts (Cont.)

Once the program has obtained values for


integer1 and integer2, it adds these values
and places the total into variable sum.

sum = integer1 + integer2; // assign total to sum


replaces whatever value was stored in sum.

11
2.4 Memory Concepts (Cont.)

This occurs when the calculated total of


integer1 and integer2 is placed into location
sum (destroying the value already in sum).
After sum is calculated, memory appears as
in Fig. 2.8.
The values of integer1 and integer2 appear
exactly as they did before they were used in
the calculation.

12
13
2.4 Memory Concepts (Cont.)

They were used, but not destroyed, as the


computer performed the calculation.

Thus, when a value is read from a memory


location, the process is said to be
nondestructive.

14
2.5 Arithmetic in C
Most C programs perform calculations using the C
arithmetic operators (Fig. 2.9).

The asterisk (*) indicates multiplication and the


percent sign (%) denotes the remainder operator, which
is introduced below.

In algebra, to multiply a times b, we simply place these


single-letter variable names side by side as in ab.

15
2.5 Arithmetic in C

In C, however, if we were to do this, ab


would be interpreted as a single, two-letter
name (or identifier).
Therefore, C requires that multiplication be
explicitly denoted by using the * operator as
in a * b.
The arithmetic operators are all binary
operators.
For example, the expression 3 + 7 contains
the binary operator + and the operands 3
and 7.
16
17
2.5 Arithmetic in C (Cont.)
Integer Division and the Remainder Operator
Integer division yields an integer result
For example, the expression 7 / 4 evaluates to 1
and the expression 17 / 5 evaluates to 3
C provides the remainder operator, %, which yields
the remainder after integer division
Can be used only with integer operands
The expression x % y yields the remainder after x
is divided by y
Thus, 7 % 4 yields 3 and 17 % 5 yields 2

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.)

Parentheses for Grouping Subexpressions


Parentheses are used in C expressions in the
same manner as in algebraic expressions.
For example, to multiply a times the
quantity b + c we write a * ( b + c ).

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.)

The rules of operator precedence specify the


order C uses to evaluate expressions. When we
say evaluation proceeds from left to right,
associativity of the
operators.

right to left.
Figure 2.10 summarizes these rules of operator

24
25
2.5 Arithmetic in C (Cont.)

Figure 2.11 illustrates the order in which


the operators are applied.

26
27
2.5 Arithmetic in C (Cont.)

unnecessary parentheses in an expression


to make the expression clearer.

These are called redundant parentheses.

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

grade on an exam is greater than or equal to


60 and whether the program should print

29
2.6 Decision Making: Equality and
Relational Operators

if statement that allows a program to make a


decision based on the truth or falsity of a
statement of fact called a condition.
If the condition is true (i.e., the condition is
met) the statement in the body of the if
statement is executed.
If the condition is false
met) the body statement is not executed.
30
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.

If the condition in any of these if


statements is true, the printf statement
associated with that if executes.

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

programs in this chapter in particular int


and if are keywords or reserved words of
the language.
Figure 2.15 contains the C keywords.
These words have special meaning to the C
compiler, so you must be careful not to use
these as identifiers such as variable names.

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.)

If you need to display a string without a


terminating newline character, use printf
with two arguments.
For example
printf( "Welcome " );
should be written as:
printf( "%s", "Welcome " );
These changes are responsible coding practices
that eliminate certain security C vulnerabilities
as we get deeper into C

54

You might also like