Chapter 4
Chapter 4
Prepared By:
Mdm. Nik Maria Nik Mahamood
Reference:
Hanly, Koffman, C Problem Solving and Program Design in C, Sixth Edition, Pearson
International Edition. Refer chapter 2 (Pg. 66 – 119) AND Chapter 3 (pg 137 – 139)
INSPIRING CREATIVE AND INNOVATIVE MINDS
DATA TYPES, OPERATORS AND LIBRARY FUNCTIONS
Constants
• Constants are data values that cannot be change during
the execution of a program.
• Defined Constants
– Use the preprocessor command define
– Example
#define SALES_TAX_RATE 0.05
• Memory Constants
– Use a C type qualifier : const
– Example:
Const float pi = 3.14159;
INSPIRING CREATIVE AND INNOVATIVE MINDS
4.2 OPERATORS
3+7
contains the binary operator + and the operands 3 and 7.
• Expression
– is a sequence of operands and operators that reduces
to a single value.
• Operator
– language-specific syntactical token that requires an
action to be taken.
• Operand
– receives an operator’s action.
Arithmetic
C Operation C expression
Operator
+ Addition 10 + 7
- Subtraction 120 – 64
* Multiplication 3 * 7
/ Division 66 / 6
% Modulus 4 % 2
Division Operation
• Two types:
– Integer division
– Floating-point division
• Both types of division using / operator.
Integer Division
• Both operands are integer values.
• Operand can be constants or variables.
• Examples:
8/2 sum / 3
98 / value sum / bil
INSPIRING CREATIVE AND INNOVATIVE MINDS
4.2 OPERATORS
C expression Output
64 / 8 8
12 / 5 2
23 / 3 7
Floating-point Division
• One operand is floating-point values or both operands
are floating-points.
• After floating-point division operation result must be
floating point
C expression Output
5 / 2.5 2.0
5.0 / 10 0.5
Modulus Operation
• Special operation.
• Output of this operation is the remainder after first
operand is divided by second operand.
• This operation can be done for integer operands only.
• The result must be an integer value.
• Example
C expression Output
5 % 3 2
7 % 2 1
12 % 2 0
Relational Operators
• Relational operators use to compare value from two
expressions.
• The result of using relational operators is always the
observation of true or false
• False (zero) and True (non-zero).
== X == Y X is equal to Y
!= X != Y X is not equal to Y
Mantic Operators
• Mantic operators use to determine a decision from
several cases (conditions) related.
• The mantic operators expressions are shown in table
below.
&& AND
|| OR
! NOT
A !A
0 1
1 0
CLASS EXERCISE
Question #1 Question #3
(A >= 1) && (B == 5) ! (C > A)
Question #2 Question #4
(C >= (B * 3)) || (A == 3) ! ((A < B) || (C > D))
Sample
Operator Explanation
expression
A++ A = A + 1 A += 1;
++A A = A + 1 A += 1;
A-- A = A - 1 A -= 1;
--A A = A - 1 A -= 1;
EXTRA NOTES :
A++ use current value of A then increment A by 1
CLASS EXERCISE ++A increment A by 1 first after then do the calculation
J = 3
Step 3 : I = 5 + 1
= 6
EXTRA NOTES :
CLASS EXERCISE A++ use current value of A then increment A by 1
++A increment A by 1 first after then do the calculation
Question #2
Answer #2:
I = 5;
I = ?
J = I –2;
I++ J = ?
Question #3
I = 5; Answer #3:
I++; I = ?
J = I – 2; J = ?
CASE STUDY
Problem
Case study: Supermarket Coin Processor
(Refer page 99 from the main textbook)
Output
Level of Precedence
• C evaluates arithmetic expressions in a precise
sequence determined by rules of operator precedence.
• Precedence of arithmetic expressions as shown in table
below:
Operator(s) Order of evaluation (Precedence)
() Evaluated first.
Evaluated second. If they are several,
*, /, %
they are evaluated left to right
Evaluated last. If they are several,
+ or -
they are evaluated left to right
Level of Precedence
• C evaluates arithmetic expressions in a precise
sequence determined by rules of operator precedence.
• Precedence of arithmetic expressions as shown in table
below:
Operator(s) Order of evaluation (Precedence)
() Evaluated first.
Evaluated second. If they are several,
*, /, %
they are evaluated left to right
Evaluated last. If they are several,
+ or -
they are evaluated left to right
2 + 5 * 3 2 * 6 / 3 (2 + 5) / 3
= 2 + 15 = 12 / 3 = 7 / 3
= 17 = 4 = 21
14 – 5 + 6 14 – 5 + 6
16 – 8 16 – 8 / 2
2
15 + 5 + 7 (15 + 5 + 7) / 2
2
+= X += Y X = X + Y
-= X -= Y X = X – Y
*= X *= Y X = X * Y
/= X /= Y X = X / Y
%= X %= Y X = X % Y
Example:
b2 – 4ac b * b – 4 * a * c
a + b
(a + b) / (c + d)
c + d
1
1 / (1 + x * x)
1 + x2
a x –(b + c) a * -(b + c)
z = pow(5, 2);
Function result, 25 is assigned to z
INSPIRING CREATIVE AND INNOVATIVE MINDS
4.5 LIBRARY FUNCTIONS
Output