0% found this document useful (0 votes)
4 views14 pages

3.operators in C

The document provides an overview of operators in C programming, including arithmetic, relational, logical, assignment, and increment/decrement operators. It explains operator precedence and associativity, along with examples and exercises for practice. The objective is to enable students to effectively use these operators in their C programs.

Uploaded by

Dilu Sandalika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

3.operators in C

The document provides an overview of operators in C programming, including arithmetic, relational, logical, assignment, and increment/decrement operators. It explains operator precedence and associativity, along with examples and exercises for practice. The objective is to enable students to effectively use these operators in their C programs.

Uploaded by

Dilu Sandalika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Certificate in Computer Science

Chandula Rajapaksa
1
Operators in C
Objective:

• At the end of the Lecture students should be able to Use


arithmetic operators in C programs.
• Correctly apply the precedence of arithmetic operators
• Use relational operators in C programs.
2
Arithmetic Operators
Operation Arithmetic Operator C Expression Example

Addition + No1 + No2 1+2=3

Subtraction - Value – No3 52 – 2 = 50

Multiplication * Num1 * 3 4 * 2.5 = 10

Division / H1 / 5 10 / 3 = 3

Remainder % No1 % no2 10 % 3 = 1

3
Operator Precedence and
Associativity
▪ Operator precedence establishes the priority of
an operator in relationship to all other operators.
▪ Parentheses can be used to modify the normal
order of execution of an expression.
▪ Operator associativity establishes the order in
which operators of the same precedence are to
be executed.
4
Operator Precedence and
Associativity
Order Operator Associativity
1 () Parenthesis Left to Right
2 * Multiplication Left to Right
/ Division
% Remainder

3 + Addition Left to Right


- Subtraction

5
Operator Precedence and
Associativity
▪ Example : 55 / 10 % 2 * 5 -10 % (4-2)

= 55 / 10 % 2 * 5 – 10 % 2
= 5 % 2 * 5 – 10 % 2
= 1 * 5 – 10 % 2
= 5 – 10 % 2
=5–0
=5 6
Operator Precedence and
Associativity
▪ Exercises :
1. 2 * 3 / 4 * 5 – 2

2. 2 * ( 3 + 4 ) % 3 + 3

3. -75 / 25 + 10 * 3

4. 50 % 3 + 2 * 2

5. 2 * 2 * 6 – 7 / 2

7
Equality & Relational Operators
▪ Equality and relational operators test the relationship between two expressions
and yields true or false
Operation Operator C Expression
Equal to == x == y
Not Equal to != x != 2
Greater than > x>6
Less than < x<y
Greater than or equal to >= x >= 9

Less than or equal to <= x <= y


8
Operator Precedence

Order Operator Associativity


1 () Left to Right
2 ! Left to Right
3 * / % Left to Right
4 + - Left to Right
5 < <= > >= Left to Right
6 == != Left to Right
7 = Left to Right
9
Logical Operators
Used to form more complex conditions by combining simple
conditions.

Operation Operator C Expression


Logical AND && A = 1 && B=2
Logical OR || Mid = 20 || Final = 70
Logical NOT ! ! (grade == ‘F’)

10
Assignment Operators
▪ There are several assignment operators for abbreviating
assignment expressions.
variable = variable operator expression;
▪ can be written as
variable operator= expression;
where operator is one of the binary operators +, -, *, / or %
Example : a = a+2 is as same as a += 2

11
Increment & Decrement
Operators
▪ ++ increment operator
▪ -- decrement operator

▪ k = ++n; // prefix increment : n = n + 1; then k = n;

▪ k = n++; // postfix increment : k = n; then n = n + 1;

▪ k = --n; // prefix decrement : n = n - 1; then k = n;

▪ k = n--; // postfix decrement : k = n; then n = n - 1;


12
▪ Exercises : x = 5 and y = 10
1. x += 2
2. printf(“%d\n”,x++); printf(“%d\n”,x);
3. printf(“%d\n”,++y); printf(“%d\n”,y);

13
14

You might also like