際際滷

際際滷Share a Scribd company logo
Operators
Precedence and Associativity of Operators
                                          Operators                                Associativity
     ( )        [ ]         .        ->            ++(postfix)      --(postfix)    left to right
     ++(prefix)         --(prefix)     !      ~    sizeof(type)
                                                                                   right to left
     +(unary)         -(unary)     &(address)   *(dereference)
                                 *           /          %                          left to right
                                +                        -                         left to right
                                <<                       >>                        left to right
                        <             <=           >          >=                   left to right
                                 ==                     !=                         left to right
                                             &                                     left to right
                                             ^                                     left to right
                                              |                                    left to right
                                             &&                                    left to right
                                             ||                                    left to right
                                             ? :                                   right to left
 =         +=   -=      *=           /=      %=        >>=    <<=   &=   ^=   |=   right to left


                                                                                                   2
Operators
 Comparison:

     C operator   Description   Example
     ==           =             a == 2
     !=           Not equal     a != 3
     <            <             a<3
     >            >             a>4
     <=                        a <= 5
     >=                        a >= 3
     !            Negative      !(a < 3)
     &&           and           (3 < a) && (a < 5)
     ||           or            (a < 3) || ( 5 < a)




                                                      3
Operators
 Arithmetic

  Operator     Description         Example
  +            +                   a+2
                                 a3
  *            * (Mutiplication)   a*3
  /            / (Division)        a/4
  %            Remainder           a%5
  ++           Increment           a++, ++a
  ==           Decrement           a--, --a




                                              4
Operators
 Assignment

    Operator   Description   Example
    =          assignment    a=b
    +=         a=a+3         a += 3
    -=         a=a-3         a -= 3
    *=         a=a*3         a *= 3
    /=         a=a/3         a /= 3
    %=         a=a%3         a %= 3




                                       5
Increment and Decrement Operators
 Increment operator
  Postfix k++
                                   k = k - 1 ;
  Prefix ++k

 Decrement operator
  Postfix k--
                                   k = k - 1 ;
  Prefix --k


         But, It is a different when use with another operator

   Prefix : increase or decrease before used in expression
   Postfix : increase or decrease after used in expression



                                                                 6
Increment and Decrement Operators

[Ex]   int i = 4, j;

       j = ++i + 3;
       printf(i : %d, j = %dn, i, j);

       j = i++ + 3;
       printf(i : %d, j = %dn, i, j);

       j = --i + 3;
       printf(i : %d, j = %dn, i, j);

       j = i-- + 3;
       printf(i : %d, j = %dn, i, j);
                                           i = 5,   j   =8
                                           i = 6,   j   =8
                                           i = 5,   j   =8
                                           i = 4,   j   =8

                                                             7
Bit Operators
 <<, >>, |, &, ^,~
   << : Left shift n places

            a = 100 << 1 ;      100 -> 01100100
            printf( %d, a )   Shift to left by 1 bit
                                11001000 (=200)

   >> : Right shift n places

            a = 100 >> 1 ;      100 -> 01100100
            printf( %d, a )   Shift to right by 1 bit
                                001100100 (=50)




                                                          8
Bit Operators
                                          (8bit Computer)
 <<, >>, |, &, ^,~
   | : Logical OR, if either bit is 1,   100 -> 01100100
        the result is 1                   200 -> 11001000
          a = 100 | 200 ;                 -------------------
          printf( %d, a )               a   = 11101100 (236)

   & : Logical AND, if both bits are 1,
                                         100 -> 01100100
       the result is 1
                                         200 -> 11001000
         a = 100 & 200 ;                 -------------------
         printf( %d, a )               a   = 01000000 (64)
   ^ : Logical XOR, if one and only
    one bit is 1, the result is 1        100 -> 01100100
         a = 100 ^ 200 ;                 200 -> 11001000
         printf( %d, a )               -------------------
                                         a   = 10101100 (172)

                                                                 9
Bit Operators
                                (8bit Computer)
 <<, >>, |, &, ^,~
   ~ : Logical inverter        100 -> 01100100
                                -------------------
            a = ~100;
                                a   = 10011011 (155)
            printf( %d, a )




                                                       10
Example
[Ex]
   i = 3 , j = 2 , k = 1 ;
   if ( i > j > k ) printf( Yes )
  else printf( No ) ;

[Ex]
   int a, i =1, j = 2, k ;
  a = i < 2 + j;
  j = 5 * (k = 3);
  printf( %d %d %dn, a, i, j, k ) ;

    ( i < j ) && ( j < k )
  This statement means that j is between i and k.


                                                    11
Example
 Example
int a = 5, b = 6, c = 7 ;
int d ;

printf( %d %d %dn, a < b, a > b, a != b ) ;
d = (a < b) && (b < c ) ;
printf( %d %d %dn, d, (a > b) || (b > c), !(!5) ) ;




                                                         12
Assignment Operators
 Compound Assignment
   Compound Assignment operators
    op= [Ex]       *=, -=, /=, %=, +=
   variable = variable op (expression)
    => variable op= expression


  [Ex]    int k = 5;
          k += 2;         /*   k = k + 2,   k=7    */
          k -= 2;         /*   k = k - 2,   k=5    */
          k *= 2;         /*   k = k * 2,   k=10   */
          k /= 2;         /*   k = k / 2,   k=5    */
          k %= 2;         /*   k = k % 2,   k=1    */




                                                        13

More Related Content

2 2. operators

  • 2. Precedence and Associativity of Operators Operators Associativity ( ) [ ] . -> ++(postfix) --(postfix) left to right ++(prefix) --(prefix) ! ~ sizeof(type) right to left +(unary) -(unary) &(address) *(dereference) * / % left to right + - left to right << >> left to right < <= > >= left to right == != left to right & left to right ^ left to right | left to right && left to right || left to right ? : right to left = += -= *= /= %= >>= <<= &= ^= |= right to left 2
  • 3. Operators Comparison: C operator Description Example == = a == 2 != Not equal a != 3 < < a<3 > > a>4 <= a <= 5 >= a >= 3 ! Negative !(a < 3) && and (3 < a) && (a < 5) || or (a < 3) || ( 5 < a) 3
  • 4. Operators Arithmetic Operator Description Example + + a+2 a3 * * (Mutiplication) a*3 / / (Division) a/4 % Remainder a%5 ++ Increment a++, ++a == Decrement a--, --a 4
  • 5. Operators Assignment Operator Description Example = assignment a=b += a=a+3 a += 3 -= a=a-3 a -= 3 *= a=a*3 a *= 3 /= a=a/3 a /= 3 %= a=a%3 a %= 3 5
  • 6. Increment and Decrement Operators Increment operator Postfix k++ k = k - 1 ; Prefix ++k Decrement operator Postfix k-- k = k - 1 ; Prefix --k But, It is a different when use with another operator Prefix : increase or decrease before used in expression Postfix : increase or decrease after used in expression 6
  • 7. Increment and Decrement Operators [Ex] int i = 4, j; j = ++i + 3; printf(i : %d, j = %dn, i, j); j = i++ + 3; printf(i : %d, j = %dn, i, j); j = --i + 3; printf(i : %d, j = %dn, i, j); j = i-- + 3; printf(i : %d, j = %dn, i, j); i = 5, j =8 i = 6, j =8 i = 5, j =8 i = 4, j =8 7
  • 8. Bit Operators <<, >>, |, &, ^,~ << : Left shift n places a = 100 << 1 ; 100 -> 01100100 printf( %d, a ) Shift to left by 1 bit 11001000 (=200) >> : Right shift n places a = 100 >> 1 ; 100 -> 01100100 printf( %d, a ) Shift to right by 1 bit 001100100 (=50) 8
  • 9. Bit Operators (8bit Computer) <<, >>, |, &, ^,~ | : Logical OR, if either bit is 1, 100 -> 01100100 the result is 1 200 -> 11001000 a = 100 | 200 ; ------------------- printf( %d, a ) a = 11101100 (236) & : Logical AND, if both bits are 1, 100 -> 01100100 the result is 1 200 -> 11001000 a = 100 & 200 ; ------------------- printf( %d, a ) a = 01000000 (64) ^ : Logical XOR, if one and only one bit is 1, the result is 1 100 -> 01100100 a = 100 ^ 200 ; 200 -> 11001000 printf( %d, a ) ------------------- a = 10101100 (172) 9
  • 10. Bit Operators (8bit Computer) <<, >>, |, &, ^,~ ~ : Logical inverter 100 -> 01100100 ------------------- a = ~100; a = 10011011 (155) printf( %d, a ) 10
  • 11. Example [Ex] i = 3 , j = 2 , k = 1 ; if ( i > j > k ) printf( Yes ) else printf( No ) ; [Ex] int a, i =1, j = 2, k ; a = i < 2 + j; j = 5 * (k = 3); printf( %d %d %dn, a, i, j, k ) ; ( i < j ) && ( j < k ) This statement means that j is between i and k. 11
  • 12. Example Example int a = 5, b = 6, c = 7 ; int d ; printf( %d %d %dn, a < b, a > b, a != b ) ; d = (a < b) && (b < c ) ; printf( %d %d %dn, d, (a > b) || (b > c), !(!5) ) ; 12
  • 13. Assignment Operators Compound Assignment Compound Assignment operators op= [Ex] *=, -=, /=, %=, += variable = variable op (expression) => variable op= expression [Ex] int k = 5; k += 2; /* k = k + 2, k=7 */ k -= 2; /* k = k - 2, k=5 */ k *= 2; /* k = k * 2, k=10 */ k /= 2; /* k = k / 2, k=5 */ k %= 2; /* k = k % 2, k=1 */ 13