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

Operators and Expressions

This document provides an overview of operators in the C programming language. It defines what operators are and lists the main categories of operators in C, including arithmetic, relational, logical, assignment, increment/decrement, conditional, and bitwise operators. It provides examples of how each operator is used and the order of precedence when multiple operators are used in an expression (BODMAS rule). The document also discusses operator associativity, semantics of logical operators, and provides examples of evaluating expressions with operators.

Uploaded by

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

Operators and Expressions

This document provides an overview of operators in the C programming language. It defines what operators are and lists the main categories of operators in C, including arithmetic, relational, logical, assignment, increment/decrement, conditional, and bitwise operators. It provides examples of how each operator is used and the order of precedence when multiple operators are used in an expression (BODMAS rule). The document also discusses operator associativity, semantics of logical operators, and provides examples of evaluating expressions with operators.

Uploaded by

LawrOot
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

OPERATORS &

EXPRESSIONS
OPERATOR
 “is a symbol (+,-,*,/) that directs the computer to perform certain
mathematical or logical manipulations and is usually used to manipulate
data and variables”
 Ex: a+b
OPERATORS IN C
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
ARITHMETIC
OPERATORS
Operator Example Meaning
+ a+b Addition –unary
- a–b Subtraction- unary
* a*b Multiplication
/ a/b Division
% a%b Modulo division- remainder
RELATIONAL OPERATORS

Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal
to
== Equal to
!= Not equal to
ASSIGNMENT OPERATORS

Syntax:
v op = exp;
where
v = variable,
op = shorthand assignment operator
exp = expression

Example:
x=x+3
x += 3
SHORTHAND ASSIGNMENT
OPERATORS
Simple assignment
operator Shorthand 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
INCREMENT & DECREMENT
OPERATORS
C supports 2 useful operators namely
1. Increment ++
2. Decrement -- operators
 The ++ operator adds a value 1 to the operand
 The -- operator subtracts 1 from the operand
 ++a or a++
 --a or a--
RULES FOR ++ & -- OPERATORS

1. These require variables as their operands


2. 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
3. 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
EXAMPLES FOR ++ & --
OPERATORS
1. Let the value of a = 5 and b = ++a then
a=b=6
2. Let the value of a = 5 and b = a++ then
a=5
but b = 6
Example
3. a prefix operator first adds 1 to the operand and then the
result is assigned to the variable on the left
4. a postfix operator first assigns the value to the variable
on left and then increments the operand.
CONDITIONAL OPERATORS

 Syntax: exp1 ? exp2 : exp3


where exp1,exp2 and exp3 are expressions
 Working of the ? Operator:
 Exp1 is evaluated first, if it is nonzero(1/true) then the expression2 is evaluated
and this becomes the value of the expression,
 If exp1 is false(0 / zero) exp3 is evaluated and its value becomes the value
of the expression
 Example:

m=2;
n=3
r=(m>n) ? m : n;
EXAMPLE: INTEGER
OPERANDS d = (x < y);
#include<stdio.h>
void main(void)
e = (x >= y);
{ f = (x <= y);
int a, b, c, d, e, f;
int x, y; cout<< “a = ”<< a<<endl;
cout<< “b = ”<< b<<endl;
x = 5;
Y = 10;
cout<< “c = ”<< c<<endl;
cout<< “d = ”<< d<<endl;
a = (x == y); cout<< “e = ”<< e<<endl;
b = (x != y); }
c = (x > y);

09/15/2023
BITWISE OPERATORS
These operators allow manipulation of data at the bit level

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
SPECIAL OPERATORS

1. Comma operator ( , )
2. sizeof operator – sizeof( )
3. Pointer operators – ( & and *)
4. Member selection operators – ( . and ->)
ARITHMETIC EXPRESSIONS
Algebraic expression C expression
1. A x b - c a*b-c
2. (m + n)(x + y) (m + n) *(x + y)
a*b/c
3.

4. 3x2+2x+1 3*x*x+2*x+1

5. a/b

6. S = (a + b + c) / 2
S=
ARITHMETIC EXPRESSIONS
Algebraic expression C expression

area = area = sqrt(s * (s - a) * (s -b) * (s - c))

Sin
sin(b / sqrt(a * a + b * b))

tow1 = sqrt((rowx - rowy) / 2 + tow * x * y * y)

tow1 = sqrt(pow ((rowx - rowy) / 2, 2) + tow * x * y * y)

y = (alpha + beta) / sin(theta * 3.1416 / 180) + abs(x)


PRECEDENCE OF
OPERATORS
(BODMAS RULE)
BRACKETS OF DIVISION
MULTIPLICATION ADDITION
SUBTRACTION(BODMAS RULE)
1. Brackets will have the highest precedence and have to be
evaluated first
2. division, multiplication, addition and finally subtraction.
 C language uses some rules in evaluating the expressions
 called as precedence rules or sometimes
 Also referred to as hierarchy of operations, with some operators with
highest precedence and some with least.
 The 2 distinct priority levels of arithmetic operators in c are-
1. Highest priority : ^ * / %
2. Lowest priority : + -
RULES FOR EVALUATION OF
EXPRESSION
1. First parenthesized sub expression from left to right are evaluated.
2. If parentheses are nested, the evaluation begins with the innermost
sub expression
3. The precedence rule is applied in determining the order of
application of operators in evaluating sub expressions
4. The associatively rule is applied when 2 or more operators of the
same precedence level appear in a sub expression.
5. Arithmetic expressions are evaluated from left to right using the
rules of precedence
6. When parentheses are used, the expressions within parentheses
assume highest priority
HIERARCHY OF OPERATORS

Operator Description Associativity


( ), [ ] Function call, array Left to Right
element reference

+, -, ++, - Unary plus, minus,


-, ! , ~ , * , & increment, decrement,
logical negation, 1’s
complement, pointer Right to Left
reference, address

*, / , % Multiplication, division, Left to Right


modulus
EXAMPLE 1
Evaluate x1 = (-b + sqrt (b * b – 4 * a * c)) / (2 * a)
Where a =1, b =- 5, c = 6
=(-(-5) + sqrt((-5) (-5) – 4 * 1 * 6)) / (2 * 1)
=(5 + sqrt((-5) (-5) – 4 * 1 * 6)) / (2 * 1)
=(5 + sqrt(25 – 4 * 1 * 6)) / (2 * 1)
=(5 + sqrt(25 – 4 * 6)) / (2 * 1)
=(5 + sqrt(25 - 24))/(2 * 1)
=(5 + sqrt(1)) / (2 * 1)
=(5 + 1.0) / (2 * 1)
=(6.0) / (2 * 1)
=6.0 / 2
= 3.0
EXAMPLE 2
Evaluate the expression when a = 4
b = a - ++a
=a–5
=5–5
=0
LOGICAL OPERATIONS IN C
! - NOT
 && - AND
 || - OR

 Normally used in conjunction with relational operators to


test for multiple conditions

09/15/2023
SEMANTICS OF THE !
OPERATOR
 !0 results in 1
 !1 results in 0

09/15/2023
SEMANTICS OF THE &&
OPERATOR
 0 && 0 results in 0
 0 && 1 results in 0
 1 && 0 results in 0
 1 && 1 results in 1

09/15/2023
SEMANTICS OF THE ||
OPERATOR
 0 || 0 results in 0
 0 || 1 results in 1
 1 || 0 results in 1
 1 || 1 results in 1

09/15/2023
SEATWORK ( WRITE YOUR
ANSWER IN A 1 WHOLE SHEET OF
PAPER)
A. Write a C program that will declare variables (using the appropriate
data type) and assign values to these variables for the following
problems. You can use any constant value for initialization purposes.
1. The net income is computed as the gross income minus the income
tax, SSS contribution and medical insurance.
2. The salary of a part time worker is computed as the product of the
hourly rate and the number of hours worked.
3. The area of a rectangle is computed as length multiplied by width.
4. The circumference of the circle is computed as 2 multiplied by PI ( a
constant value approximately as 3.1416) multiplied by the radius.
5. Compute the sum of the integer values 1,3,5,7,8, and 9.

09/15/2023
B.DETERMINE IF THE
STATEMENT IF TRUE OR
FALSE
1. (‘ A ’ > ‘ B ’)
2. (123 == 321)
3. (1 + 2 + 3 >= 3 + 2 + 1)
4. (5.0 <= 10.0 / 2.0)
5. (3 + 5 * 10 != 3 * 5 +10)
6. (‘ A ‘ == 65)

09/15/2023

You might also like