01/08/11 By Jag PRESENTATION  ON OPERATORS IN C
Topics Concept of Operators Classification of operators 01/08/11 By Jag
Classification   Arithmetic operators Conditional operators Bitwise operators Relational operators Logical operators Assignment operators Increment and decrement operators Special operators 01/08/11 By Jag
Arithmetic Operators Integer Arithmetic  Real Arithmetic  Mixed –mode  Arithmetic 01/08/11 By Jag
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 01/08/11 By Jag
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); } 01/08/11 By Jag
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 01/08/11 By Jag
Mixed –mode  Arithmetic One of the operands is real and the other is integer  For example 19/10.0=1.9 01/08/11 By Jag
Conditional operators The conditional operator consists of  2  symbols the question mark (?) and the colon (:)  Syntax: exp1 ? exp2: exp3 01/08/11 By Jag
Example   01/08/11 By Jag a=16 b=25; x=(a>b) ? a : b; if (a>b)  x=a; else    x=b;
Bitwise operators 01/08/11 By Jag Operator Meaning & Bitwise AND  |  Bitwise OR  ^  Bitwise Exclusive << Shift left  >>  Shift right
Result of logical Bitwise Operation 01/08/11 By Jag 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 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 01/08/11 By Jag
Bitwise Exclusive OR x - - -> 0000 0000 0000 1101 y - - -> 0000 0000 0001 1001 x^y - - -> 0000 0000 0001 0100 01/08/11 By Jag
Bitwise shift operators Left shift op<<n Eg.  0100 1001 1100  1011 x<<3 Right shift op>>n 01/08/11 By Jag
Bitwise Complement Operators ~ one’s complement operator is a unary operator. 01/08/11 By Jag
Relational Operators 01/08/11 By Jag 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 01/08/11 By Jag Operator Meaning  &&  Logical AND  ||  Logical OR  !  Logical NOT
Examples Logical AND (&&)    a > b && x = = 10  Logical OR (||)  a < m || a < n Logical NOT (!) ! (x >= y) 01/08/11 By Jag
Assignment Operators  In addition, C has a set of shorthand assignment operators of the form.  var oper = exp;    Example   x = a + b  01/08/11 By Jag
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. 01/08/11 By Jag
Special operators Comma operator  Size of operator Pointer operators (& and *)  Member selection operators (. and ->). 01/08/11 By Jag
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++)  01/08/11 By Jag
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);  01/08/11 By Jag
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. 01/08/11 By Jag
Summary of C operator  01/08/11 By Jag 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…. 01/08/11 By Jag 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
01/08/11 By Jag

C ppt

  • 1.
    01/08/11 By JagPRESENTATION ON OPERATORS IN C
  • 2.
    Topics Concept ofOperators Classification of operators 01/08/11 By Jag
  • 3.
    Classification Arithmetic operators Conditional operators Bitwise operators Relational operators Logical operators Assignment operators Increment and decrement operators Special operators 01/08/11 By Jag
  • 4.
    Arithmetic Operators IntegerArithmetic Real Arithmetic Mixed –mode Arithmetic 01/08/11 By Jag
  • 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 01/08/11 By Jag
  • 6.
    Use of integerarithmetic main() { int months, days; printf(“Enter days\n”); scanf(“%d, &days”); months=days/30; days=days%30; printf(“Months=%d Days=%d”, months, days); } 01/08/11 By Jag
  • 7.
    Real Arithmetic Realoperator 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 01/08/11 By Jag
  • 8.
    Mixed –mode Arithmetic One of the operands is real and the other is integer For example 19/10.0=1.9 01/08/11 By Jag
  • 9.
    Conditional operators Theconditional operator consists of 2 symbols the question mark (?) and the colon (:) Syntax: exp1 ? exp2: exp3 01/08/11 By Jag
  • 10.
    Example 01/08/11 By Jag a=16 b=25; x=(a>b) ? a : b; if (a>b) x=a; else x=b;
  • 11.
    Bitwise operators 01/08/11By Jag Operator Meaning & Bitwise AND | Bitwise OR ^ Bitwise Exclusive << Shift left >> Shift right
  • 12.
    Result of logicalBitwise Operation 01/08/11 By Jag 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
  • 13.
    Example Bitwise ANDx - - -> 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 01/08/11 By Jag
  • 14.
    Bitwise Exclusive ORx - - -> 0000 0000 0000 1101 y - - -> 0000 0000 0001 1001 x^y - - -> 0000 0000 0001 0100 01/08/11 By Jag
  • 15.
    Bitwise shift operatorsLeft shift op<<n Eg. 0100 1001 1100 1011 x<<3 Right shift op>>n 01/08/11 By Jag
  • 16.
    Bitwise Complement Operators~ one’s complement operator is a unary operator. 01/08/11 By Jag
  • 17.
    Relational Operators 01/08/11By Jag 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
  • 18.
    Logical operators 01/08/11By Jag Operator Meaning && Logical AND || Logical OR ! Logical NOT
  • 19.
    Examples Logical AND(&&) a > b && x = = 10 Logical OR (||) a < m || a < n Logical NOT (!) ! (x >= y) 01/08/11 By Jag
  • 20.
    Assignment Operators In addition, C has a set of shorthand assignment operators of the form. var oper = exp; Example x = a + b 01/08/11 By Jag
  • 21.
    Increment and Decrementsyntax: 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. 01/08/11 By Jag
  • 22.
    Special operators Commaoperator Size of operator Pointer operators (& and *) Member selection operators (. and ->). 01/08/11 By Jag
  • 23.
    Comma operator Linkrelated 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++) 01/08/11 By Jag
  • 24.
    The sizeof OperatorGives 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); 01/08/11 By Jag
  • 25.
    Precedence and AssociativityPrecedence 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. 01/08/11 By Jag
  • 26.
    Summary of Coperator 01/08/11 By Jag 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
  • 27.
    Continue…. 01/08/11 ByJag 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
  • 28.