Lecture Notes Operators in C Programming
Lecture Notes Operators in C Programming
Topics
• Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Increment and Decrement Operators
1
Operators
• An operator is a symbol, which helps the user to
command the computer to do a certain mathematical
or logical manipulations.
• Operators are used in programming language
program to operate on data and variables.
• Operands are the values that we perform operation on
those values.
•
2
Precedence:
If there is more than one operator in an expression, which
operation we need to perform first is specified by
precedence.
Associativity:
If more than one operator has the same precedence
which operation we need to perform is specified by
associativity.
3
Types of operators based on number of operands:
We have three types of operators
1. Unary Operators – require one operand.
4
Operators can be classified as:
1. Arithmetic operators.
2. Relational operators.
3. Logical operators.
4. Assignment operators.
6. Conditional operators.
7. Bitwise operators.
8. Special operators.
5
Arithmetic Operators
• You can use an arithmetic operator with one or two
arguments to add, subtract, multiply, and divide
numeric values.
Operator Name Description
6
/ Division to divide one number by
another.
Arithmetic Operators
Example:
i. 5 + 3 = 8
7
ii. 5 – 3 = 2
iii.5 * 3 = 15
iv.5 / 3 = 1
v. 5 % 3 = 2
Arithmetic Operators
• *, / and % will be performed before + or - in any
expression.
• Brackets can be used to force a different order of
evaluation to this.
8
Arithmetic Operators
Example
i. 2+5*4–3=?
ii. (2 + 5) * (4 – 3) = ?
Arithmetic Operators
• Here are some arithmetic expressions used within
assignment statements:
9
i. z = x + y ii. no1 = x – y
iii. age = a * b + c iv.
velocity = distance / time
v. force = mass * acceleration vi.
count = count + 1
Integer Arithmetic
• When an arithmetic operation is performed on two
whole numbers or integers than such an operation is
called as integer arithmetic.
• It always gives an integer as the result.
10
Integer Arithmetic
Example
i. x + y = 32 ii.
x – y = 22 iii.
x * y = 115 iv.
11
x % y = 2 v.
x/y=5
Floating-point Arithmetic
• When an arithmetic operation is preformed on two real
numbers or fraction numbers such an operation is
called floating-point arithmetic.
12
Floating-point Arithmetic
Example
i. x + y = 18.0 ii.
x – y = 10.0 iii.
x * y = 56.0 iv.
x / y = 3.50
13
Unary minus example: -2, 2*-1
14
Example: 15/4=3 ii. In real
arithmetic, all operands are real.
Example: 15.0/4.0=3.75 iii. In mixed mode arithmetic,
one operand is real and another operand is integer then
that expression is called mixed mode arithmetic.
15
Example program:
#include<stdio.h>
main()
{
int a,b;
floatc,d;
printf("enter 2 integer values\n");
scanf("%d%d",&a,&b);
printf("enter 2 float values\n");
scanf("%f%f",&c,&d);
16
printf("Integer Arithmetic
%d/%d=%d\n",a,b,a/b);
printf("real Arithmetic %f/%f=%f\n",c,d,c/d);
printf("Mixed mode Arithmetic
%f/%d=%f\n",c,b,c/b);
}
Relational Operators
• An operator that compares two values.
• For example, the expression:
17
x<5
means x is less than 5
18
• Expressions that contain relational operators are
called relational expressions.
• A simple relational expression contains only one
relational operator and takes the following form:
<exp1> relational operator <exp2>
19
Operator Name Description
Indicates whether the value of the left
< Less than operand is less than the value of the
right operand.
Indicates whether the value of the left
<= Less than or equal to operand is less than or equal to the
value of the right operand.
Indicates whether the value of the left
> Greater than operand is greater than the value of the
right operand.
Indicates whether the value of the left
Greater than or equal operand is greater than or equal to the
>= to value of the right operand.
20
Operator Name Description
Indicates whether the value of the left
== Equal to operand is equal to the value of the right
operand.
Indicates whether the value of the left
!= Not equal to operand is not equal to the value of the
right operand.
21
Relational Operators
Example:
22
Logical Operators
• An operator that compare or evaluate logical and
relational expressions.
• The following are logical operators:
Operator Name
|| Logical OR
! Logical NOT
23
Local NOT ---------- highest precedence, Associativity from Right-> Left
Logical AND ------------- next highest precedence, Associativity from Left -> Right Logical OR ----
--------- lowest precedence, Associativity from Left -> Right
Logical AND
• This operator is used to evaluate two conditions or
expressions with relational operators simultaneously.
• If both the expressions to the left and to the right of the
logical operator is true then the whole compound
expression is true.
Exp1 Exp2 Exp1 && Exp2
24
False False False
True False False
False True False
True True True
25
Logical AND
Example:
26
Logical AND
Example:
27
iii. (a > b) && (c == 5) =
False
iv. (a < b) && (b < c) = True
Logical OR
• The logical OR is used to combine two expressions or the
condition evaluates to true if any one of the 2 expressions
is true.
• The expression evaluates to true if any one of them is true
or if both of them are true.
28
False False False
True False True
False True True
True True True
Logical OR
Example:
(a < m) || (a < n)
29
The expression evaluates to true if any one of them is
true or if both of them are true.
Logical OR
Example:
30
iii. (a > b) || (c == 5) = True
iv. (a < b) || (b < c) = True
Logical NOT
• The logical NOT operator takes single expression and
evaluates to true if the expression is false and
evaluates to false if the expression is true.
• In other words it just reverses the value of the
expression.
Exp1 !Exp1
31
True False
False True
Logical NOT
Example:
! (x >= y)
32
Logical NOT
Example:
33
Assignment Operators
Assignment operators are used to assign the result of an
expression to a variable. Assignment operator is '='.
Example: a=10;
Left hand side must be a variable; Right hand side may
be a variable/constant or combination of these two.
34
v op=exp;
Where v is a variable, exp is an expression and op is C
binary arithmetic operator.
The operator op=is known as the shorthand
assignment operator. The assignment
statement vop= exp;
is equivalent to v
= v op (exp); Associativity is from
Right to Left.
35
Statement with Statement
simple with
assignment shorthand
operator operator
a =a + 100 a += 100
a = a - 20 a -= 20
a = a * (n+1) a *= (n+1)
a = a / (n+1) a /= (n+1)
a=a%b a %= b
36
The use of shorthand assignment operators has three
advantages:
1. What appears on the left hand side need not be
Example program:
#include<stdio.h>
main()
{
37
int a,n;
printf("enter a,n
values\n");
scanf("%d%d",&a,&n);
a += 100;
printf("a=%d\n",a); a -
= 20; printf("a=%d\n",a);
a *= (n+1);
printf("a=%d\n",a);
38
a /= (n+1);
printf("a=%d\n",a);
a %= n;
printf("a=%d\n",a);
}
Increment and Decrement Operators
• 'C' has two very useful operators not generally found
in other languages. These are increment and
decrement operators: ++ and --
• The operator ++ adds 1 to the operand.
39
• The operator -- subtracts 1 from the operand.
• Both are unary operators.
• We use the increment and decrement statements in
for and while loops extensively.
• The syntax of the operators is given below:
++ variable name
variable name++ –
–variable name
variable name– –
40
Statement with
Operator Meaning
41
Increment and Decrement Operators
Example:
42
m = 5; y = ++m; Suppose if we rewrite the above
(prefix) statement as
43
Increment and Decrement Operators
• A prefix operator first adds 1 to the operand and then
the result is assigned to the variable on the left.
• On the other hand, a postfix operator first assigns
the value to the variable on the left and then
increments the operand.
Statement with
Operator Meaning
44
--a Pre decrement
a-- Post decrement
x=4y=
++x
PRINT x
PRINT y
45
What is the output?
5
5
Increment and Decrement Operators
Example 2:
x=3y=
x++
PRINT x
PRINT y
46
What is the output?
4
3
Increment and Decrement Operators
Example 3:
x=3y
= --x
PRINT x
PRINT y
47
What is the output?
2
2
Increment and Decrement Operators
Example 4:
x=3y
= x--
PRINT x
PRINT y
48
What is the output?
2
3
49