0% found this document useful (0 votes)
50 views49 pages

Chapter 4

This document discusses data types, operators, and library functions in C programming. It covers basic data types like char, int, double, and float and their typical sizes. It also explains arithmetic, relational, and logical operators in C like +, -, ==, &&, and ||. Examples are provided to demonstrate how to use operators to perform calculations and comparisons in C code. Library functions are also briefly mentioned as a topic that will be covered.
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)
50 views49 pages

Chapter 4

This document discusses data types, operators, and library functions in C programming. It covers basic data types like char, int, double, and float and their typical sizes. It also explains arithmetic, relational, and logical operators in C like +, -, ==, &&, and ||. Examples are provided to demonstrate how to use operators to perform calculations and comparisons in C code. Library functions are also briefly mentioned as a topic that will be covered.
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/ 49

DDWC1573 - PROGRAMMING FUNDAMENTAL

CHAPTER 4: DATA TYPES, OPERATORS AND


LIBRARY FUNCTIONS

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

4.1 Representation of Data Types


4.2 Operators
4.3 Arithmetic Operations
4.4 Writing Mathematical Formulas in C
4.5 Library Functions

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.1 REPRESENTATION OF DATA TYPES

• Variables can represents various of data types.


• The basic of data types can be divided into two classes:
– Character
• Data type : char
• A character in C can be interpreted as a small
integer (between 0 and 255).
– Numeric
• Data type: int, float and double
• Integer (int) type – a number without fraction.
• Data type float and double – a number with fraction
(floating-point).

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.1 REPRESENTATION OF DATA TYPES

• C contains standard data types:


– char
– int
– double
– float

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.1 REPRESENTATION OF DATA TYPES

Data Type - int


• The integer data type represents a number without
fraction (floating points).
– In C, is called int.
– Example:
- 10500 435 +15 –25 32767

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.1 REPRESENTATION OF DATA TYPES

Typical integer sizes :


Type Byte size Range value
short int 2 -32767 .. 32767
unsigned short int 2 0 .. 65535
int 2 -32767 .. 32767
unsigned int 2 0 .. 65535
long int 4 -2147483647 .. 2147483647
unsigned long int 4 0 .. 4294967295

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.1 REPRESENTATION OF DATA TYPES

Data Type - double / float


• The double or float data type represents a number with
fraction.
– Example: 123.45 0.0005 3.1478

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.1 REPRESENTATION OF DATA TYPES

Typical floating sizes :


Byte Floating-
Type Range value
size point
float 4 6 10-37 .. 1038
double 8 15 10-307 .. 10308
long double 16 19 10-4931 .. 104932

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.1 REPRESENTATION OF DATA TYPES

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

• Most of C programs perform arithmetic calculations.


• Better know operators to manipulate data.
• The arithmetic operators are all binary operators.
• For example, the expression

3+7
contains the binary operator + and the operands 3 and 7.

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

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

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

• The C Arithmetic Operators are summarized in table below :

Arithmetic
C Operation C expression
Operator

+ Addition 10 + 7

- Subtraction 120 – 64

* Multiplication 3 * 7

/ Division 66 / 6

% Modulus 4 % 2

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

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

• After integer division operation result must be integer.


• Example

C expression Output

64 / 8 8

12 / 5 2

23 / 3 7

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

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.5 / 2.0 2.25

5.0 / 10 0.5

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

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

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

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

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

• The Relational Operators are summarized in table below:


Relational Example of c
Meaning of C Condition
Operator condition

== X == Y X is equal to Y

!= X != Y X is not equal to Y

> X > Y X is greater than Y

< X < Y X is less than Y

>= X >= Y X is greater than or equal to Y

<= X <= Y X is less than or equal to Y

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

• The examples of Relational Expressions are as follow:

Relational Expression Value


6 < 9 1
7 == 5 0
6 != 5 1
9 >= 5 1

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

Mantic Operators
• Mantic operators use to determine a decision from
several cases (conditions) related.
• The mantic operators expressions are shown in table
below.

Mantic Operator Meaning

&& AND

|| OR

! NOT

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

• The examples of Mantic Operations are shown in table below:


A B A && B A || B
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1

A !A
0 1
1 0

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

CLASS EXERCISE

• Given A = 2, B = 5, C = 15, D = 17, answer questions below.

Question #1 Question #3
(A >= 1) && (B == 5) ! (C > A)

Question #2 Question #4
(C >= (B * 3)) || (A == 3) ! ((A < B) || (C > D))

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

Increment and Decrement Operators


• C provides the unary increment operator ++ and the
unary decrement operator --
• These operators are summarized in table below.
Sample
Operator Explanation
expression
Increment a by 1, then use the new
++ ++a value of a in the expression in
which a resides.
Use the current value of a in the
++ a++ expression in which a resides, then
increment a by 1.

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

Sample
Operator Explanation
expression

Decrement b by 1, then use the new


-- --b value of b in the expression in
which b resides.

Use the current value of b in the


-- b-- expression in which b resides,
then decrement b by 1.

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

• The Increment and Decrement Expressions are summarized


in table below:

Expression Meaning Statement

A++ A = A + 1 A += 1;

++A A = A + 1 A += 1;

A-- A = A - 1 A -= 1;

--A A = A - 1 A -= 1;

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

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

Question #1 Solution Question #1


I = 5;
Step 1 : I = 5
J = I++ - 2
Step 2 : J = I++ - 2
Answer #1: = 5 – 2
I = 6 = 3

J = 3
Step 3 : I = 5 + 1
= 6

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

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 = ?

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

CASE STUDY

Problem
Case study: Supermarket Coin Processor
(Refer page 99 from the main textbook)

This case study demonstrates the manipulation of type int


data using / and % and type char data

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.2 OPERATORS

Output

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

• C arithmetic expression is a combination one or more


arithmetic expressions.
• Examples
5+6
14 * 5 * 2
21 – 6 + 7 * 4 / 2
7.5 * (2.0 – 4.52)

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

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

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

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

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

• Example of C Arithmetic Expressions based on Precedence


Evaluation.

Example #1: Example #2: Example #3:

2 + 5 * 3 2 * 6 / 3 (2 + 5) / 3

= 2 + 15 = 12 / 3 = 7 / 3

= 17 = 4 = 21

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

• Comparison Between Algebraic Expression and C Arithmetic


Expression
Algebraic Expressions C Arithmetic Expressions

14 – 5 + 6 14 – 5 + 6

16 – 8 16 – 8 / 2
2

15 + 5 + 7 (15 + 5 + 7) / 2
2

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

The Assignment Expression


• evaluates the operand on the right side of the operator (=)
• The result places the expression values in the operand on
the left of assignment operators
• Examples
Average = (6 + 8 + 5) / 3;
Salary = 16.5 * 30;
X = X + 1;
Z = 5;

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

/* Example 1: simple assignment expression */


#include <stdio.h>
int main() {
double price = 12.50, discount = 0.05, payment;
payment = price – (price * discount);
printf(“Price is RM %.2lf\n”, payment);
}

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

/* Example 2: assignment expression input from user */


#include <stdio.h>
int main() {
double price, discount, payment;
printf(“The price of purchase: “);
scanf(“%lf”, &price);
printf(“Rate of discount: “);
scanf(“%lf”, &discount);
payment = price – (price * discount);
printf(“Price is RM %.2lf\n”, payment);
}
INSPIRING CREATIVE AND INNOVATIVE MINDS
4.3 ARITHMETIC EXPRESSIONS

Double Assignment Expressions


• Example:
a = b = c = 40;

This statement can be solved by:


a = b = (c = 40)
a = (b = (c = 40))
(a = (b = (c = 40)))

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.3 ARITHMETIC EXPRESSIONS

Compound Assignment Expressions


• A compound assignment is a shorthand notation for a
simple assignment.
• Five compound assignment operators are discussed:
+=, *=, /=, %=, and -=
• Example
simple expression given as follows:
bil = bil + 5;
using compound assignment operator:
bil += 5;
INSPIRING CREATIVE AND INNOVATIVE MINDS
4.3 ARITHMETIC EXPRESSIONS

• The Compound Assignment Operators are summarized in


table below:

Compound Compound Equivalent simple


Operator Expression Expression

+= 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

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.4 WRITING MATHEMATICAL FORMULA IN C

Example:

Mathematical Formula C Expression

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)

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.5 LIBRARY FUNCTIONS

• C has no operator symbols meaning “square root”,


“power” or “exponent”.
• C provides program unit called functions to carry out
these and other mathematical operations.
• These functions also known as predefined functions.
• Example:
y = sqrt(100);
Function result, 10 is assigned to y

z = pow(5, 2);
Function result, 25 is assigned to z
INSPIRING CREATIVE AND INNOVATIVE MINDS
4.5 LIBRARY FUNCTIONS

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.5 LIBRARY FUNCTIONS

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.5 LIBRARY FUNCTIONS

INSPIRING CREATIVE AND INNOVATIVE MINDS


4.5 LIBRARY FUNCTIONS

Output

INSPIRING CREATIVE AND INNOVATIVE MINDS

You might also like