Slide1: 12/2/2016 1 PRESENTATION ON OPERATORS IN C
Topics: 12/2/2016 2 Topics Concept of Operators Classification of operators
Classification : 12/2/2016 3 Classification Arithmetic operators Conditional operators Bitwise operators Relational operators Logical operators Assignment operators Increment and decrement operators Special operators
Arithmetic Operators: 12/2/2016 4 Arithmetic Operators Integer Arithmetic Real Arithmetic Mixed –mode Arithmetic
Integer Arithmetic : 12/2/2016 5 Integer Arithmetic Operands in a single arithmetic expression Operation is integer arithmetic E.g. If a =14 and b=4 a-b=10 a+b=18 a/b=3 a%b=2
Use of integer arithmetic: 12/2/2016 6 Use of integer arithmetic main() { int months, days; printf(“Enter days\n”); scanf(“%d, &days”); months=days/30; days=days%30; printf(“Months=%d Days=%d”, months, days); }
Real Arithmetic: 12/2/2016 7 Real Arithmetic Real operator is known as real arithmetic. Decimal and exponential notation If x,y are floats x=6.0/7.0=0.857143 y=-2.0/3.0=-0.666667
Mixed –mode Arithmetic: 12/2/2016 8 Mixed –mode Arithmetic One of the operands is real and the other is integer For example 19/10.0=1.9
Conditional operators: 12/2/2016 9 Conditional operators The conditional operator consists of 2 symbols the question mark (?) and the colon (:) Syntax: exp1 ? exp2: exp3
Example: 12/2/2016 10 Example a=16 b=25; x=(a>b) ? a : b; if (a>b) x=a; else x=b;
Bitwise operators: 12/2/2016 11 Bitwise operators Operator Meaning & Bitwise AND | Bitwise OR ^ Bitwise Exclusive << Shift left >> Shift right
Result of logical Bitwise Operation: 12/2/2016 12 Result of logical Bitwise Operation op1 op2 op1&op2 op1|op2 op1^op2 1 1 1 1 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0
Example: 12/2/2016 13 Example Bitwise AND x - - -> 0000 0000 0000 1101 y - - -> 0000 0000 0001 1001 x&y- - -> 0000 0000 0000 1001 Bitwise OR x - - - > 0000 0000 0000 1101 y - - -> 0000 0000 0000 1001 x|y - - -> 0000 0000 0001 1101
Bitwise Exclusive OR: 12/2/2016 14 Bitwise Exclusive OR x - - -> 0000 0000 0000 1101 y - - -> 0000 0000 0001 1001 x^y - - -> 0000 0000 0001 0100
Bitwise shift operators: 12/2/2016 15 Bitwise shift operators Left shift op<<n Eg. 0100 1001 1100 1011 x<<3 Right shift op>>n
Bitwise Complement Operators: 12/2/2016 16 Bitwise Complement Operators ~ one’s complement operator is a unary operator.
Relational Operators: 12/2/2016 17 Relational Operators Operator Meaning < 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
Logical operators : 12/2/2016 18 Logical operators Operator Meaning && Logical AND || Logical OR ! Logical NOT
Examples: 12/2/2016 19 Examples Logical AND (&&) a > b && x = = 10 Logical OR (||) a < m || a < n Logical NOT (!) ! (x >= y)
Assignment Operators : 12/2/2016 20 Assignment Operators In addition, C has a set of shorthand assignment operators of the form. var oper = exp; Example x = a + b
Increment and Decrement: 12/2/2016 21 Increment and Decrement syntax: 1. ++variable name 2. variable name++ 3. – –variable name 4. variable name– – x= 5; y = ++x; (prefix) In this case the value of y and x would be 6 x= 5; y = x++; (post fix) Then the value of y will be 5 and that of x will be 6.
Special operators : 12/2/2016 22 Special operators Comma operator Size of operator Pointer operators (& and *) Member selection operators (. and ->).
Comma operator: 12/2/2016 23 Comma operator Link related expressions together Expressions are evaluated left to right for e.g. value = (x = 10, y = 5, x + y); for (n=1, m=10, n <=m; n++, m++)
The sizeof Operator: 12/2/2016 24 The sizeof Operator Gives of bytes occupied in the memory. To determine the lengths of arrays and structures when their sizes are not known to the programmer for e.g. x = sizeof (sum); y = sizeof (long int); z= sizeof (235L);
Precedence and Associativity : 12/2/2016 25 Precedence and Associativity Precedence rules decides the order in which different operator are applied. Associativity rule decides the order in which multiple occurrences of the same level operator are applied.
Summary of C operator : 12/2/2016 26 Summary of C operator Description Operator Rank Associativity Function call Array element reference ( ) [] 1 Left to right Unary plus Unary minus Increment Decrement Logical negation Ones complement Address Size of an object + - ++ -- ! ~ & Sizeof 2 Right to left Multiplication Division Modulus * / % 3 Left to right Addition Subtraction + - 4 Left to right Left shift Right shift << >> 5 Left to right Less than Less than equal to Greater than Greater than equal to < <= > >= 6 Left to right Equality Inequality = = |= 7 Left to right
Continue….: 12/2/2016 27 Continue…. Bitwise AND & 8 Left to right Bitwise XOR ^ 9 Left to right Bitwise OR | 10 Left to right Logical AND && 11 Left to right Logical OR || 12 Left to right Conditional operator ?: 13 Right to left Assignment operator = *=/=%= +=-=&= ^=|= << = >>= 14 Right to left Commas operator , 15 Left to right
Slide28: 12/2/2016 28