Operators and Operands
• Arithmetic Operator
• Unary operators
• Relational and logical operators
• Assignment operators and
• Conditional operator
• The data items that operators act upon, are called
operands.
Arithmetic Operators
• There are five arithmetic operators in C. They are
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Reminder after integer division
• There is no exponentiation operator in C.
• There is a library function (POW) under math.h to
carry out exponentiation
Arithmetic Operators (cont.)
• The remainder operator (%) requires that both operands be integers
and the second operand be nonzero.
• Similarly, the division operator (/) requires that the second operand
be nonzero.
a+b 13
a-b 7
a*b 30
a/b 3 Here, the decimal portion of the
quotient will be dropped
a%b 1
a+b 14.5
a-b 10.5
a*b 25.0
a/b 6.25
a%b Not Possible!!!
a 65
a+b 131
a+1 66
a + ‘A’ 130
a + ‘1’ 114
int a, b;
a = 11;
b = -3;
Follow
basic
rules of
a+b 8 algebra
a-b 14
a*b -33
a/b -3
a%b 2 Ignore - !!!
Type Convention
• Operands that differ in type may undergo type conversion before the
expression takes on its final value.
• In general, the final result will be expressed in the highest precision
possible, consistent with the data types of the operands.
• Rules apply when neither operand is unsigned.
We will learn detail
later
int i = 7; ASCII Value
float f = 5.5; w = 119
char c = ‘w’ 0 = 48
i+f 12.5 float (double)
i+c 126 integer
i + c – ‘0’ 78 integer
(i + c) - (2 * f / 5) 123.8 float (double)
• Type conversion
Type Convention (Assignment)
• If the two operands in assignment expression are of different data
types.
• The value of right hand operand will automatically be converted to
the type of the operand on the left.
• The entire assignment expression will be then same data type.
We will learn
detail later
float
Left
int 11.995
Right
float
int
3 float
Left
3.0
int
11
;
Type Cast
• To transform the type of a variable temporarily.
• To do so, the expression must be preceded by the name of the
desired data type, enclosed in parentheses
(data type) expression
int number;
(float) number;
Valid or Invalid?
i = 7;
f = 8.5;
result = (i + f) % 4; Invalid
• Type conversion 2
Valid or Invalid?
float num = 10.5;
num % 2;
float num = 10.5;
((int)num) % 2;
Operators and Operands
• Arithmetic Operator
• Unary operators
• Relational and logical operators
• Assignment operators and
• Conditional operator
Unary Operators
• A class of operators that act upon a single operand to produce a
new value.
• Known as unary operators.
Unary Operators… We will learn detail
later
• Perhaps the most common unary operation is unary minus (-8)
• There are two other commonly used unary operators:
• The increment operator, ++
• The decrement operator, --
• sizeof - returns the size of its operand, in bytes.
• Cast - (type) expression
Relational Operators
• There are four relational operators in C. They are
Equality Operators
• For equality
Relational Operator
• The relational operators compare two values
and return a true or false result based upon
that comparison.
Operator Meaning Type
< Less than Relational
> Greater than Relational
<= Less than or equal to Relational
>= Greater than or equal to Relational
== Equal to Equality
!= Not equal to Equality
Relational Operators…
• These six operators are used to form logical expressions, which
represent conditions that are either true or false.
• The resulting expressions will be of type integer
• True is represented by the integer value 1
• False is represented by the value 0
Examples of Conditions
Operator Condition Meaning
<= x <= 0 x less than or equal
to 0
< Power < MAX_POW Power less than
MAX_POW
== letter == ‘M’ Letter equal to ‘M’
!= num != x num not equal to x
4-23
i=1
True or False j=2
k=3
Expression Result Value
i<j true 1
(1 + j) >= k true 1
(j + k) > (i + 5) false 0
i=1
True or False j=2
k=3
Expression Result Value
k != 3 false 0
j == 2 true 1
(j + k) >= (i + 5) false 0
Simplified Expression
Logical Operation
• There are three kinds of logical operators.
True or False!
i=7
True or False f = 5.5
c = ‘w’ (119)
Expression Result Value
(i >= 6) && (c == 'w') true 1
(i >= 6) || (c == 119) true 1
(f < 11) && (i > 100) false 0
Operator Precedence
An operator’s precedence determines its order of
evaluation.
Operator Precedence
An operator’s precedence determines its order of evaluation.
Evaluated as: ++x is executed first (x = 6), then x++ (x = 6, then x = 7)
Operators and Operands
• Arithmetic Operator
• Unary operators
• Relational and logical operators
• Assignment operators and
• Conditional operator
Assignment operators
• Most common “=“
• = and == are not same!
• Other five are:
• +=
• -=
• *=
• /=
• %=
Assignment operators….
• expression 1 += expression 2
is equivalent to
• expression 1 = expression 1 + expression 2
Example: sum = sum + N; sum+=N;
Similarly, the assignment expression
• expression I -= expression 2
is equivalent to
• expression 1 = expression I - expression 2