C Programming: Operators and Expressions
C Programming: Operators and Expressions
Structured Programming
Lecture 5
Lecture 5
Lecture Contents
Lecture 5
Operands
• An operand specifies an entity on
which an operation is to be performed.
• An operand can be a variable name, a
constant.
• For example, a=b+2 is a valid
expression involving three operands
namely
– Two variables name i.e. a,b
– a constant i.e. 2.
Lecture 5
Operators
Lecture 5
Expressions
• An expression in C is made up of one or more
operands.
• For example,
– a=2+3 // meaningful expression
– Involves three operands :a, 2 and 3
– Two operators i.e. = (assignment operator) and +
(arithmetic addition operator).
• Thus, an expression is a sequence of operands and
operators that specifies the computation of a
value.
Lecture 5
Simple VS compound
Simple Compound
An expression that has only one An expression that involves more than one
operator is known as simple operator is called compound expression.
expression.
Lecture 5
Arithmetic Operators
Lecture 5
Arithmetic Operators (cont’d)
Lecture 5
Arithmetic Operators
Lecture 5
Arithmetic Operators
Lecture 5
Integer division vs Real division
• Division between two integers results in an
integer.
• The result is truncated, not rounded
• Example:
– int A=5/3; 🡪 A will have the value of 1
– int B=3/6; 🡪 B will have the value of 0
• To have floating point values:
– double A=5.0/3; 🡪 A will have the value of 1.666
– double B=3.0/6.0; 🡪 B will have the value of 0.5
Lecture 5
Division
• If both operands of a division
expression are integers, you will get an
integer answer. The fractional portion
is thrown away.
• Examples :
– 17 / 5 = 3
–4 / 3 = 1
– 35 / 9 = 3
Lecture 5
Division (con’t)
• Division where at least one operand is a
floating point number will produce a
floating point answer.
• Examples :
– 17.0 / 5 = 3.4
– 4 / 3.2 = 1.25
– 35.2 / 9.1 = 3.86813
• What happens? The integer operand is
temporarily converted to a floating point,
then the division is performed.
Lecture 5
Division By Zero
• Division by zero is mathematically
undefined.
• If you allow division by zero in a
program, it will cause a fatal error.
Your program will terminate execution
and give an error message.
• Non-fatal errors do not cause
program termination, just produce
incorrect results.
Lecture 5
Modulus
• The expression m % n yields the integer
remainder after m is divided by n.
• Modulus is an integer operation -- both
operands MUST be integers.
• Examples :17 % 5 = 2
● 6%3 = 0
● 9%2 = 1
● 5%8 = 5
Lecture 5
Uses for Modulus
Lecture 5
Rules of Operator
Precedence
Operator(s) Precedence & Associativity
Lecture 5
Examples
Mixed operations:
int a=4+6/3*2; 🡪 a=? a= 4+2*2 = 4+4 = 8
int b=(4+6)/3*2; 🡪 b=? b= 10/3*2 = 3*2= 6
Lecture 5
Extended Example
Lecture 5
Arithmetic Expressions
Algebraic expression C expression
axb-c a*b-c
(m+n)(x+y) (m+n)*(x+y)
a*b/c
3x2+2x+1 3*x*x+2*x+1
a/b
S=
S=(a+b+c)/2
Lecture 5
Relational Operators
• Relational operators compare two operands to
produce a boolean result i.e. 0 or 1.
• In C any non-zero value (1 by convention) is
considered to be ’true’ and 0 is considered to be
false.
Lecture 5
Assignment Operators
• Another common expression type found while
programming in C that assign value.
• Form
– var = var (op) expr
Lecture 5
Assignment Operators
• We can use some short form of assignment operator
• Ex: x=x+3
x+=3
Simple assignment
Shorthand operator
operator
a = a+1 a + =1
a = a-1 a - =1
a = a* (m+n) a * = m+n
a = a / (m+n) a / = m+n
a = a %b a %=b
Lecture 5
Increment & Decrement Operators
Lecture 5
Increment & Decrement Operators
• When postfix either ++ or – – is used with
the variable in a given expression, the
expression is evaluated first and then it is
incremented or decremented by one.
• When prefix either ++ or – –is used with the
variable in a given expression, it is
incremented or decremented by one first
and then the expression is evaluated with
the new value.
Lecture 5
More but important
Lecture 5
Increment & Decrement Operators
Lecture 5
Example
Lecture 5
Logical operators
Lecture 5
Logical Operators
Lecture 5
Logical operators
Lecture 5
Lecture 5