C ppt

23,286 views

Published on

3 Comments
18 Likes
Statistics
Notes
No Downloads
Views
Total views
23,286
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
1,486
Comments
3
Likes
18
Embeds 0
No embeds

No notes for slide

C ppt

  1. 1. 01/08/11 By Jag PRESENTATION ON OPERATORS IN C
  2. 2. Topics Concept of Operators Classification of operators 01/08/11 By Jag
  3. 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. 4. Arithmetic Operators Integer Arithmetic Real Arithmetic Mixed –mode Arithmetic 01/08/11 By Jag
  5. 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. 6. Use of integer arithmetic main() { int months, days; printf(“Enter daysn”); scanf(“%d, &days”); months=days/30; days=days%30; printf(“Months=%d Days=%d”, months, days); } 01/08/11 By Jag
  7. 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 01/08/11 By Jag
  8. 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. 9. Conditional operators The conditional operator consists of 2 symbols the question mark (?) and the colon (:) Syntax: exp1 ? exp2: exp3 01/08/11 By Jag
  10. 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. 11. Bitwise operators 01/08/11 By Jag Operator Meaning & Bitwise AND | Bitwise OR ^ Bitwise Exclusive << Shift left >> Shift right
  12. 12. 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
  13. 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 01/08/11 By Jag
  14. 14. Bitwise Exclusive OR x - - -> 0000 0000 0000 1101 y - - -> 0000 0000 0001 1001 x^y - - -> 0000 0000 0001 0100 01/08/11 By Jag
  15. 15. Bitwise shift operators Left shift op<<n Eg. 0100 1001 1100 1011 x<<3 Right shift op>>n 01/08/11 By Jag
  16. 16. Bitwise Complement Operators ~ one’s complement operator is a unary operator. 01/08/11 By Jag
  17. 17. 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
  18. 18. Logical operators 01/08/11 By Jag Operator Meaning && Logical AND || Logical OR ! Logical NOT
  19. 19. Examples Logical AND (&&) a > b && x = = 10 Logical OR (||) a < m || a < n Logical NOT (!) ! (x >= y) 01/08/11 By Jag
  20. 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. 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. 01/08/11 By Jag
  22. 22. Special operators Comma operator Size of operator Pointer operators (& and *) Member selection operators (. and ->). 01/08/11 By Jag
  23. 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++) 01/08/11 By Jag
  24. 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); 01/08/11 By Jag
  25. 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. 01/08/11 By Jag
  26. 26. 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
  27. 27. 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
  28. 28. 01/08/11 By Jag

×