(Lecture 4) Operators and Expression - 2025
(Lecture 4) Operators and Expression - 2025
C Programming Language
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.
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).
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
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.
➢ 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);
➢ 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);
Operator Meaning
== Equal to
!= not equal to
23
Relational and Logical Operators
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
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
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
34
Using the Conditional Operator
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