SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Arrow 101 - Kotlin funcional com Arrow
Arrow 101 - Kotlin funcional com ArrowArrow 101 - Kotlin funcional com Arrow
Arrow 101 - Kotlin funcional com ArrowLeandro Ferreira
 
C programming operators
C programming operatorsC programming operators
C programming operatorsSuneel Dogra
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms StacksManishPrajapati78
 
Adding numbers without using the + operator
Adding numbers without using the + operatorAdding numbers without using the + operator
Adding numbers without using the + operatorkiwipom
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms QueuesManishPrajapati78
 
Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - DatabaseShahadat153031
 
Application linear function
Application linear functionApplication linear function
Application linear functionpuspitaaya
 
Stack1
Stack1Stack1
Stack1Iqrazb
 
Python operators part2
Python operators part2Python operators part2
Python operators part2Vishal Dutt
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of PrecedenceMuhammad Hammad Waseem
 
Munihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMunihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMatthew Pickering
 

Mais procurados (16)

Arrow 101 - Kotlin funcional com Arrow
Arrow 101 - Kotlin funcional com ArrowArrow 101 - Kotlin funcional com Arrow
Arrow 101 - Kotlin funcional com Arrow
 
Operator 04 (js)
Operator 04 (js)Operator 04 (js)
Operator 04 (js)
 
C programming operators
C programming operatorsC programming operators
C programming operators
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Unit 5 review
Unit 5 reviewUnit 5 review
Unit 5 review
 
Adding numbers without using the + operator
Adding numbers without using the + operatorAdding numbers without using the + operator
Adding numbers without using the + operator
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
 
Functions
FunctionsFunctions
Functions
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms Queues
 
Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - Database
 
Application linear function
Application linear functionApplication linear function
Application linear function
 
Stack1
Stack1Stack1
Stack1
 
Python operators part2
Python operators part2Python operators part2
Python operators part2
 
1D Array
1D Array1D Array
1D Array
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
Munihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMunihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template Haskell
 

Semelhante a 2 2. operators

Semelhante a 2 2. operators (20)

Java - Operators
Java - OperatorsJava - Operators
Java - Operators
 
Java 2
Java 2Java 2
Java 2
 
Operators inc c language
Operators inc c languageOperators inc c language
Operators inc c language
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
operators.ppt
operators.pptoperators.ppt
operators.ppt
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
C operators
C operators C operators
C operators
 
The expression evaluation is compiler dependent, and may vary. A g.pdf
The expression evaluation is compiler dependent, and may vary. A g.pdfThe expression evaluation is compiler dependent, and may vary. A g.pdf
The expression evaluation is compiler dependent, and may vary. A g.pdf
 
Comma versus list
Comma versus listComma versus list
Comma versus list
 
Operators
OperatorsOperators
Operators
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Operators
OperatorsOperators
Operators
 
Operators in Python
Operators in PythonOperators in Python
Operators in Python
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Operators in java
Operators in javaOperators in java
Operators in java
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
Operators in C & C++ Language
Operators in C & C++ LanguageOperators in C & C++ Language
Operators in C & C++ Language
 
SPL 6 | Operators in C
SPL 6 | Operators in CSPL 6 | Operators in C
SPL 6 | Operators in C
 

Mais de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 

Mais de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
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