0% found this document useful (0 votes)
26 views

Lecture Notes Operators in C Programming

The document discusses different types of operators used in C programming. It describes arithmetic operators like addition, subtraction, multiplication, division etc. Relational operators like less than, greater than, equal to are used to compare values. Logical operators like AND, OR, NOT are used to combine relational expressions. Increment and decrement operators are used to increase or decrease a value by 1. The precedence and associativity of different operators are also specified.

Uploaded by

Sandra Lymo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lecture Notes Operators in C Programming

The document discusses different types of operators used in C programming. It describes arithmetic operators like addition, subtraction, multiplication, division etc. Relational operators like less than, greater than, equal to are used to compare values. Logical operators like AND, OR, NOT are used to combine relational expressions. Increment and decrement operators are used to increase or decrease a value by 1. The precedence and associativity of different operators are also specified.

Uploaded by

Sandra Lymo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

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.

2. Binary Operators- require two operands.

3. Ternary Operators – require three operands.

4
Operators can be classified as:
1. Arithmetic operators.

2. Relational operators.

3. Logical operators.

4. Assignment operators.

5. Increment and decrement 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

+ Addition to add two numbers together

- Subtraction to subtract one number from


another

* Multiplication to multiply one number by


another.

6
/ Division to divide one number by
another.

% Modulus to find the remainder from


(Remainder) dividing 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

Let x = 27 and y = 5 be two integer numbers. Then the


integer operation leads to the following results:

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

Let x = 14.0 and y = 4.0 then

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

Note: Modulo division operator (%) is used only for


integral values and cannot be used on floating point data.

*, /, % ------ highest precedence


+, - ------ lowest precedence

Arithmetic operator’s associativity is from Left to Right.

i. In integer arithmetic, all operands are integer.

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.

If either operand is of the real type, then only the real


operation is performed and the result is always a real
number.
Example: 15.0/4=3.75 or
15/4.0=3.75

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

• This expression will have a value of TRUE if the


variable x is less than 5; otherwise the value of the
expression will be FALSE.
Relational Operators
• Relational operators are sometimes called
comparison operators.

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>

• Where exp1 and exp2 are expressions, which may be


simple constants, variables or combination of them.
Relational Operators
• The following are relational operators:

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.

<, <=, >, >= ---------------- highest precedence


==,!= ---------------- lowest precedence

21
Relational Operators
Example:

Let x = 2 and y = 5 then


i. x<y = True
ii. (x + 2) > (y * 2) = False
iii. (x + 3) <= y = True
iv. x != y = True
v. y > (3 + x) = False

22
Logical Operators
• An operator that compare or evaluate logical and
relational expressions.
• The following are logical operators:

Operator Name

&& Logical AND

|| 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

op1 op2 op1 && op2 op1 || op2


Non-zero Non-zero 1 1
Non-zero 0 0 1
0 Non-zero 0 1
0 0 0 0

25
Logical AND
Example:

(a > b) && (x == 10)

The expression to the left is a > b and that on the right is


x == 10, the whole expression is true only if both
expressions are true i.e., if a is greater than b and x is
equal to 10.

26
Logical AND
Example:

Given a = 2, b = 3 and c = 5, evaluate the following logical


expressions:
i. (a > b) && (c != 5) =
False
ii. (a < b) && (c < b) =
False

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.

Exp1 Exp2 Exp1 || Exp2

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:

Given a = 2, b = 3 and c = 5, evaluate the following logical


expressions:
i. (a > b) || (c != 5) = False
ii. (a < b) || (c < b) = True

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)

The NOT expression evaluates to true only if the value of


x is neither greater than or equal to y

32
Logical NOT
Example:

Given a = 2, b = 3 and c = 5, evaluate the following logical


expressions:

a) !(a > b) = True


b) !(a < b) = False
c) !(a > b || c == 5) = False

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.

'C' has a set of 'shorthand' assignment operators of the


form

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

repeated and therefore it becomes easier to write.


2. The statement is more concise and easier to read.

3. The statement is more efficient.

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

++a Pre increment


a++ Post increment
--a Pre decrement
a-- Post decrement

Increment and Decrement Operators


• The increment operator ++ adds the value 1 to the
current value of operand.
• The decrement operator – – subtracts the value 1 from
the current value of operand.

41
Increment and Decrement Operators
Example:

Consider the following:

42
m = 5; y = ++m; Suppose if we rewrite the above
(prefix) statement as

In this case the value of y and m = 5; y = m++;


m would be 6. (postfix)

Then the value of y will be 5 and


that of m will be 6.

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

++a Pre increment


a++ Post increment

44
--a Pre decrement
a-- Post decrement

Increment and Decrement Operators


Example 1:

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

You might also like