Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operators & Expressions

Similar presentations


Presentation on theme: "Operators & Expressions"— Presentation transcript:

1 Operators & Expressions
1

2 Operators C supports rich set of operators.
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.

3 Types of ‘C’ operators Arithmetic operators Relational operators
Logical operators Assignment operators Increment and Decrement operators Conditional operators Bitwise operators Special operators

4 1. Arithmetic operator + Addition - Subtraction * Multiplication
/ Division % Modulo division

5 2. Relational operator C supports six Relational Operators
< Is less than <= Is less than or equal to > Is greater than >= Is greater than or equal to == Is equal to != Is not equal to

6 3.Logical operators Logical Operators
&&, || and ! are the three logical operators. expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero. expr1 || expr2 has a value 1 if expr1 and expr2 both are nonzero. !expr1 has a value 1 if expr1 is zero else 0. Example if ( marks >= 40 && attendance >= 75 ) grade = ‘P’ If ( marks < 40 || attendance < 75 ) grade = ‘N’

7 4. Assignment operators Assignment operators are used to assign the result of an expression to a variable. C has a set of ‘shorthand’ assignment operator (op=): v op =exp; Exam - a += 3 ; a = a + 3 ; Both are same.

8 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

9 5. Increment and decrement operators.
Increment Operator ++ a=10; a++ or a+1=11; Decrement Operator -- b=5; b-- or b-1=4; Similarly increment and decrement operator is used in subscripted variables as: a[ i++]=5; is equivalent to a[ i]=5; i=i+1;

10 6. Conditional operator C supports a ternary operator i.e. conditional operator. The conditional operator is defined as “ ? : ” The conditional operator works in following format: for example: a=11; b=20; x=(a>b) ? a : b;

11 7. Bitwise operator C supports bitwise operators for manipulation of data at bit level. Bitwise operators may not be applied to float or double. Bitwise operators are as follows: & bitwise AND | bitwise OR ^ bitwise exclusive OR << shift left >> shift right ~ One’s Complements

12 8. Special operator C supports some special operators such as:
comma operator “,” x=(a=5,b=6) size of operator “sizeof()” Address operator “&” pointer operator “*” member selection operator “. and -> ”

13 More about operators Unary operator Binary operator Ternary operator

14 Precedence of operators
Precedence establishes the hierarchy of one set of operators over another when an arithmetic expression has to be evaluated. It refers to the order in which c evaluates operators. The evaluation of operators in an arithmetic expression takes place from left to right for operators having equal precedence . Exam. k = 2 * 3/4 + 4/4 + 8–2 + 5/8 ; O/P => 8

15 Precedence of operators
BODMAS RULE- Brackets of Division Multiplication Addition Subtraction Brackets will have the highest precedence and have to be evaluated first, then comes of , then comes division, multiplication, addition and finally subtraction. C language uses some rules in evaluating the expressions and they r 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- Highest priority : * / % Lowest priority : + -

16 Associativity of operators
Associativity tells how an operator associates with its operands. for eg: 1. The unary minus associates minus with the quantity to its right. 2. The assignment operator = associates from right to left. Hence the expression on the right is evaluated first and its value is assigned to the variable on the left. Associativity also refers to the order in which c evaluates operators in an expression having same precedence. Such type of operator can operate either left to right or vice versa. The operator () function call has highest precedence & the comma operator has lowest precedence All unary , conditional & assignment operators associate RIGHT TO LEFT . All other remaining operators associate LEFT TO RIGHT

17 Rules for evaluation of expression
First parenthesized sub expression from left to right are evaluated. If parentheses are nested, the evaluation begins with the innermost sub expression The precedence rule is applied in determining the order of application of operators in evaluating sub expressions The associatively rule is applied when 2 or more operators of the same precedence level appear in a sub expression. Arithmetic expressions are evaluated from left to right using the rules of precedence When parentheses are used, the expressions within parentheses assume highest priority

18 Hierarchy of operators
Description Associativity ( ), [ ] Function call, array element reference Left to Right +, -, ++, - -,!,~,*,& Unary plus, minus, increment, decrement, logical negation, 1’s complement, pointer reference, address Right to Left *, / , % Multiplication, division, modulus


Download ppt "Operators & Expressions"

Similar presentations


Ads by Google