0% found this document useful (0 votes)
2 views42 pages

(Lecture 4) Operators and Expression - 2025

The document provides an overview of operators and expressions in the C programming language, detailing various categories such as arithmetic, unary, relational, logical, assignment, and conditional operators. It explains the difference between operators and operands, the concept of operator precedence, and includes examples of how these operators are used in expressions. Additionally, it highlights the importance of library functions in C and includes exercises for further understanding.

Uploaded by

osobsolape12
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)
2 views42 pages

(Lecture 4) Operators and Expression - 2025

The document provides an overview of operators and expressions in the C programming language, detailing various categories such as arithmetic, unary, relational, logical, assignment, and conditional operators. It explains the difference between operators and operands, the concept of operator precedence, and includes examples of how these operators are used in expressions. Additionally, it highlights the importance of library functions in C and includes exercises for further understanding.

Uploaded by

osobsolape12
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/ 42

Operators and Expressions in

C Programming Language

Week 4  Lecture Note


The CSC121/COS121 Team @ 2025
2
RECOMMENDED Text
➢ Byron S. Gottfried “Theory and
Problems of Programming with
C” Second edition’ SCHAUM’S
Outline Series, McGRAW –HILL
(1996).

✓ Chapter 3: “Operators &


Expressions in C.”

3
Learning Objectives
▪ To understand what an operator is in C language.
▪ To differentiate between operators and operands in a C
program.
▪ To examine in details the different categories of operators
and how each is used to form expressions and statements in
a C program.
▪ To understand how the different categories of operators in C
can be used in solving problems.
▪ To highlight Library Functions in C language.
4
Lecture Outline
1. What is an Operator
2. Operators vs. Operands
3. Categories of Operators in C language.
▪ Arithmetic operators
▪ Unary operators
▪ Relational and logical operators
▪ Assignment operators
▪ Conditional operators
4. Library Functions in C language.
5
What is an OPERATOR in C
❖ An operator is a character (symbol) in C programming that is
used together with the individual elements (e.g. variables,
constants, array elements & function references) of a program
in order to form an expression or a statement, within that
program.

❖ C language includes a large number of operators which fall into


several different categories.
▪ These categories of operators can be used to form expressions and statements in C.
6
OPERATORS vs. OPERANDS
❖ An operator is a character (symbol) in C programming that is
used together with a program element to form an expression.

❖ This ‘element’ is referred to as the operand.


❖ Operands is the data that operators act upon in an expression
or a statement.
▪ While some operators require two (2) operands, some operators act just on one (1)
operand.
▪ Some operators allow the individual operand to be expressions. A few operators
permit only single variables as operands (more about this later).

7
Categories of OPERATORS in C
❖Categories of Operators in C language.
▪ Arithmetic operators
▪ Unary operators
▪ Relational and logical operators
▪ Assignment operators
▪ Conditional operators
8
Arithmetic operators
▪ C supports basic arithmetic operators. They are:
Operator Purpose
+ Addition Operator
- Subtraction Operator
* Multiplication Operator
/ Division Operator
% Remainder Operator (value after integer division) or
modulus
9
Arithmetic Operators and Operands
❖ Operands: the data that operators act upon
▪ The operands acted upon here must represent numeric values
(e.g. integer quantities, floating-point quantities, or characters).

❖ To carry out arithmetic operations requires:


▪ at-least two (2) operands,
▪ individual operands can be expressions,
▪ / and % requires second operand to be non-zero.
10
Example 1
➢ Suppose that a and b are integer variables whose values
are 10 and 3, respectively. The result is
Expression Value
<<< Note:
a+b 13 1. Division of one integer quantity by
another is referred to as integer
a-b 7 division. It returns a whole number.
a*b 30 2. Division with two floating-point
a/b 3 numbers, or with one floating-point
number and one integer, results in a
a%b 1 floating-point quotient.
11
Example 2
➢ Suppose that v1 and v2 are float variables whose values
are 12.5 and 2.0, respectively. The result is
Expression Value

v1+v2 14.5
V1-v2 10.5
v1*v2 25.0
v1/v2 6.25
12
Example 3: Arithmetic Operators and Character-
type variables
➢ Suppose that d1 and d2 are character-type variables
that represents P and T respectively. The result is

Expression Value
<<< Note:
d1+d2 164 Refer to ASCII character set,
P is encoded as (decimal) 80,
d1+d2+5 169 T is encoded as 84, and 5 is
d1+d2+‘5’ 217 encoded as 53.

13
Example 4: Operands with Negative Value
➢ If one or both operands represent negative values, then
usual rules of algebra is applied.
▪ Suppose that a and b are integer variables whose values are 11
and -3, respectively. Expression Value
a+b 8
a-b 14
a*b -30
a/b -3
a%b 2
14
Example 4a: Operands with different datatype
➢ Suppose that i is an integer variable whose value is 7, f is a
floating-point variable whose value is 5.5,and c is a character-type
variable that represents the character w. w is encoded as (decimal)
119 and 0 is encoded as 48 in the ASCII character set.
Expression Value
i+f 12.5 double-precision
i+c 126 integer
i+c-‘0’ 78 integer
(i+c)-(2*f/5) 123.8 double- precision
15
Example 5: Type Conversion
❖ Data type of variables (operands) can be converted at run
time. This is called “cast”.
❖ Format:
➢ (data type) expression

▪ Suppose that i is an integer variable whose value is 7, and f is a


floating-point variable whose value is 8.5. The expression:
▪ (i+f)%4 (will return is invalid because (i+f) is floating-point rather
integer).
▪ ((int)(i+f))%4 (result = 3)
16
Arithmetic Order of Precedence

❖Execution of arithmetic operators follows


precedence order as in BODMAS. The arithmetic
expression:
a-b/c*d
is equivalent to the algebraic formula [a-(b/c)*d].

17
Arithmetic Order of Precedence
➢ Class Exercise: Consider the arithmetic
expression:
2*((i%5)*(4+(j-3)/(k+2)))
where i , j and k are integer variables & are
assigned the values 8, 15 and 4, respectively.

What is the result?


18
Unary Operators
❖ These are operators that act upon a single operand to
produce a new value.
➢ Unary operators in C is as follows:
Operator Name Example of Use
Unary minus -743,
-3*(x + y)
Pre-increment ++counter
Post-increment counter++
Pre-decrement --counter
Post-decrement counter--
19
Example 6
➢ A C program includes an integer variable i whose initial value is 1.

➢ Suppose that the program includes the following three printf statements:
▪ printf("i=%d\n“, i);
▪ printf("i=%d\n", ++i);
▪ printf("i=%d\n", i);

▪ These statements will generate the following three lines of output.


✓ i = l
✓ i = 2
✓ i = 2
20
Example 6a
➢ A C program includes an integer variable i whose initial value is 1.

➢ Suppose that the program includes the following three printf statements:
▪ printf("i=%d\n“, i);
▪ printf("i=%d\n", i++);
▪ printf("i=%d\n", i);

▪ These statements will generate the following three lines of output.


✓ i = l
✓ i = 1
✓ i = 2
21
Relational and Logical Operators
❖ There are four relational operators in C. They are:
Operator Meaning

< Less than


<= Less than or equal to
> Greater than
>= Greater than or equal to
22
Relational and Logical Operators
▪ Closely associated with the relational operators
are the following equality operators.

Operator Meaning

== Equal to
!= not equal to
23
Relational and Logical Operators

❖The relational and equality operators are used to


form logical expressions, which represent
conditions that are either true or false.
▪ The resulting expression will be of type
integer, since true is represented by integer 1
and false is represented by the integer 0
24
Examples of Relational Operators Usage

➢Suppose the i, j, and k are integer


variables whose values are 1, 2 and 3,
respectively.
➢Several logical expressions involving
these variables are shown in the slide that
follows:
25
Examples of Relational Operators
Expression Interpretation Value
i<j true 1
(i+j)>=k true 1
(j+k)>(i + 5) false 0
k!=3 false 0
j==2 true 1
26
Logical Operators
❖ In addition to the relational and equality operators, C contains
two logical operators. These operators are referred to as
logical and and logical or respectively, and
▪ act upon operands that are themselves logical expressions. They are:

Operator Meaning
&& and
|| or
27
Examples of Logical Operators
▪ Suppose that i is an integer variable whose
value is 7, f is a floating-point variable whose
value is 5.5, and c is a character variable that
represents the character ‘w’ .
▪ Several logical expressions involving these
variables are shown in the slide that follows:
28
Examples of Relational Operators Usage

Expression Interpretation Value


(i>=6) && (c=='w') true 1
(i>=6) || (c==119) true 1
(f<11) && (i>100) false 0
(c!='p') || ((i+f)<=10) true 1

29
Assignment Operators
❖There are several different assignment
operators in C but the most commonly used
assignment operator is =.
❖Assignment expressions that make use of this
operator are written in the form:
identifier = expression
30
Assignment Operators
▪ Where identifier generally represents a variable,
and expression represents a constant, a variable
or a more complex expression.
▪ Examples include:
A=3
x=y
area=length*width
31
Assignment Operators

❖It is important to note that the assignment


operator = and the equality operator == are
distinctly different.
▪ The assignment operator is used to assign a value to
an identifier, whereas the equality operator is used to
determine if two expressions have the same value.

32
Assignment Operators
❖ Note: Automatic type conversion can result in an alteration of
the data being assigned.
▪ For example:
▪ A floating-point value may be truncated if assigned to an integer
identifier.
▪ A double-precision value may be rounded if assigned to a
floating-point (single-precision) identifier.
▪ An integer quantity may be altered if assigned to a shorter
integer identifier or to a character identifier (some high-order
bits may be lost).
33
The Conditional Operator

❖Simple conditional operations can be carried out


with the conditional operator (?:)

▪ It is written in the form:


Expression1? Expression2 : expression3

34
Using the Conditional Operator

❖In the conditional expression shown


below, assume that i is an integer variable:
(i<0)?0:100
➢The expression thus reads if i is less than
0 then value is 0 otherwise 100
35
Library Functions in C

▪ The C language is accompanied by a


number of library functions that carry
out various commonly used
operations or calculations.

36
Library functions in C

37
Library functions in C (2)

38
Exercises 1
a) What is an operator? Describe several different
categories of operators that are included in C language.
b) What is an operand? What is the relationship between
operators and operands?
c) What is meant by operator precedence?
d) Describe two different ways to utilize the increment and
decrement operators. How do the two methods differ?
39
Exercises 2
➢ Suppose a, b and c are integer variables that have
been assigned the values a=8, b=3 and c=-5.
Determine the value of each of the following
arithmetic expressions:
▪ 2*b+3*(a-c)
▪ a*b/c
▪ a*(b/c)
▪ (a*c)%b
40
Exercises 3
➢ A C program contains the following declarations:
▪ int i, j;
▪ long ix;
▪ short s;
▪ float x;
▪ double dx;
▪ char c;
Determine the data type of each of the following expressions give reason.
(a) i+c (d) dx+x
(b) s+j (e) ((int)dx)+ix
(c) x+c (f) ix+j
41
Exercises 4
➢ A C program contains the following declarations and
initial assignments:
▪ int i=8, j=5; char c ='c', d =‘d';
▪ float x = 0.005, y = -0.01;
Determine the value of each of the following expressions.
Use the values initially assigned to the variables for each expression.
A. (3*i-2* j)%(2*d-c) D. 2*x+(y==0)
B. 2*((i/5)+(4*(j-3))%(i + j - 2)) E. !(c==99)
C. (i > 0)||(j < 5) F. (x > y)&&(i>0)||(j<5)

42

You might also like