SlideShare 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
  –            –                   a–3
  *            * (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

PDF
4. operators in c programming by digital wave
PPTX
Basic c operators
PPT
6 operators-in-c
PPTX
F# intro
PDF
Operators
PPTX
C operators
4. operators in c programming by digital wave
Basic c operators
6 operators-in-c
F# intro
Operators
C operators

What's hot (16)

PPTX
Arrow 101 - Kotlin funcional com Arrow
PPTX
Operator 04 (js)
DOC
C programming operators
PPT
Data Structure and Algorithms Stacks
PDF
Unit 5 review
PDF
Adding numbers without using the + operator
PPTX
Aggregate function
PPTX
Functions
PPT
Data Structure and Algorithms Queues
PPTX
Aggregate Function - Database
PPT
Application linear function
PPTX
Stack1
PPTX
Python operators part2
PDF
1D Array
PDF
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
PDF
Munihac 2018 - Beautiful Template Haskell
Arrow 101 - Kotlin funcional com Arrow
Operator 04 (js)
C programming operators
Data Structure and Algorithms Stacks
Unit 5 review
Adding numbers without using the + operator
Aggregate function
Functions
Data Structure and Algorithms Queues
Aggregate Function - Database
Application linear function
Stack1
Python operators part2
1D Array
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Munihac 2018 - Beautiful Template Haskell
Ad

Similar to 2 2. operators (20)

PDF
Arithmetic instructions
DOCX
รายงานการเขียนคำสั่งควบคุมแบบวนซ้ำ กลุ่ม 4 ม. 6 ห้อง2
PPTX
c programme
PPT
Basic c operators
PDF
Micro Blaze C Reference
PPTX
Embedded systems
PDF
PPTX
Shunting yard algo
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
PPT
PPT
Java - Operators
PPS
C programming session 02
DOC
Unit 4
PPTX
operators_group_2[1].pptx PLEASE DOWNLOAD
PPTX
Intro to c chapter cover 1 4
PPT
C ppt
PPTX
Operators and Expression
PPTX
Operators expressions-and-statements
PPT
Operators in C
PPTX
introduction to c programming and C History.pptx
Arithmetic instructions
รายงานการเขียนคำสั่งควบคุมแบบวนซ้ำ กลุ่ม 4 ม. 6 ห้อง2
c programme
Basic c operators
Micro Blaze C Reference
Embedded systems
Shunting yard algo
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Java - Operators
C programming session 02
Unit 4
operators_group_2[1].pptx PLEASE DOWNLOAD
Intro to c chapter cover 1 4
C ppt
Operators and Expression
Operators expressions-and-statements
Operators in C
introduction to c programming and C History.pptx
Ad

More from 웅식 전 (20)

PDF
15 3. modulization
PDF
15 2. arguement passing to main
PDF
14. fiile io
PDF
13. structure
PDF
12 2. dynamic allocation
PDF
12 1. multi-dimensional array
PDF
11. array & pointer
PDF
10. pointer & function
PDF
9. pointer
PDF
7. variable scope rule,-storage_class
PDF
6. function
PDF
5 2. string processing
PDF
5 1. character processing
PDF
15 1. enumeration, typedef
PDF
4. loop
PDF
3 2. if statement
PDF
3 1. preprocessor, math, stdlib
PDF
2 3. standard io
PDF
2 2. operators
PDF
2 1. variables & data types
15 3. modulization
15 2. arguement passing to main
14. fiile io
13. structure
12 2. dynamic allocation
12 1. multi-dimensional array
11. array & pointer
10. pointer & function
9. pointer
7. variable scope rule,-storage_class
6. function
5 2. string processing
5 1. character processing
15 1. enumeration, typedef
4. loop
3 2. if statement
3 1. preprocessor, math, stdlib
2 3. standard io
2 2. operators
2 1. variables & data types

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 – – a–3 * * (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