Operators and Expressions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

• An operator is a symbol that tells the computer to

perform certain mathematical or logical


manipulations
• Operators are used in programs to manipulate
data and variables
• C operators can be classified into a number of
categories. They include

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.
 Operators offer the following properties

A) Precedence
B) Associativity
 It is one of the most important properties of the operator.
 Precedence refers to the priority alloted to the operator for
processing.
 An expression contains many operators then the operations are
carried out according to the priority of the operators
predetermined.
 Higher priority operator assessed first.
 Example
*/% ---- Highest precedence
+- ---- Lowest precedence
 Example

8 + 9 * 2 – 10
The operator * is the highest priority. Hence
multiplication operations is performed First.
=8 + 18 – 10
+ and – are having priority. In this situation,
the left most operation is solved first.

=26 – 10
=16
 When an expression having operators with
equal precedence then the associativity
property decides which operation is
performed first.
 Associavity means the direction of execution.
 Classified as two types.

1) Left to Right
2) Right to Left
 In this type expression evaluation starts from left to
right direction.

Example
12 * 4 / 8 % 2
In this types of` expression, all operators having
same precedence so associativity rule is followed.

=48 / 8 % 2
=6% 2
=0
 In this type expression evaluation starts from left to
right direction

Example
X=8 + 5 % 2
=8+1
=9
 Example

1 ) 5 * 4 + 8 / 2;
2 ) ( 8 / ( 2 * ( 2 * 2 ) ) );

Note:
If there are some sets of parenthesis in the
expression, the inner most parenthesis will be
solved first followed by the second and so on.
Precedence and associativity of operators
Precedence Operator Operation Associativity
level
1 () Function call Left to Right
[] Array subscript Left to Right
. Dot Left to Right
-> Arrow Left to Right
2 ! Logical NOT Right to Left
~ One’s complement Right to Left
- Unary minus (negation) Right to Left
++ Increment Right to Left
-- Decrement Right to Left
& Address of Right to Left
* Indirection Right to Left
(data_type) Cast operator Right to Left
sizeof Size of Right to Left

3 * Multiplication Left to Right


/ Division Left to Right
% Modulus Left to Right
Precedence and associativity of operators
Precedence Operator Operation Associativity
level
4 + Addition Left to Right
- Subtraction Left to Right

5 << Left shift Left to Right


>> Right shift Left to Right

6 < Less than Left to Right


<= Less than or equal to Left to Right
> Greater than Left to Right
>= Greater than or equal to Left to Right

7 == Equal to Left to Right


!= Not equal to Left to Right
8 & Bitwise AND Left to Right
Precedence and associativity of operators
Precedenc Operator Operation Associativity
e
level
9 ^ Bitwise XOR Left to Right
10 | Bitwise OR Left to Right
11 && Logical AND Left to Right
12 || Logical OR Left to Right
13 ?: Conditional Right to Left
14 = += -= Simple and
*= Compound Right to Left
/= %= >>= Assignment
<<= &= ^=
|=

15 , Comma Left to Right


if (x == 10 + 15 && y < 10)
 The precedence rule says that the addition operator has a higher
priority than the logical operator (&&) and the relational operator (
== and < ).
 The addition of 10 and 15 is executed first
if (x == 25 && y < 10)
 the next step is to determine x is equal to 25 and y is less than 10.
 If we assume a value of 20 for x and 5 for y, then
x == 25 is FALSE (0)
y < 10 is TRUE (1)
 Since operator < have higher priority compared to ==, y < 10 is tested
first and then x == 25 is tested
if (FALSE && TRUE)
 Because on of the condition is false, the complex condition is FALSE
 In the case of &&, it is guaranteed that the second operand will not
be evaluated if the first is zero
 In the case of ||, the second operand will not be evaluated if the first
is non-zero
 Many mathematical functions are frequently
used in analysis of real-life problems
 Most of the C compilers support these basic
math function and are defined in math
library
 To use any of these functions in a program,
we should include the line
#include <math.h>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy