Precedence of Operators / C language operator precedence / Order of precedence in C language

Now for proceeding to the next lesson of Decision Making Capability of C language, we must have some basic knowledge about theimportance of taking any decision or how importance is it in C language.Decisions are always the most important thing in our lives also, take anyexample from our lives, if someone is roaming in a new place and what he onlyknows is his destination, so when he would read the sign board that theparticular way leads to his desired place he would definitely choose that wayover others. Now relating it to the C language, when we (as a programmer) givechoice to user for more than one paths, then the user have the choice, whichway he wants to choose. The more you give the ease to user for variety ofdecisions the more effective your programming is. It simply asks the user to:

The higher your level of programming, the more complex programming you could to enchance the effectiveness of your programming.

Complex programming is just like complex highways.

Here’s the list of C++ operators according to theirprecedence.

Pre.  Operator           Description                                   Example                 Associativity

1        ::                        Scoping Operator                         Class::age = 2;     none 2 ()

2        ()                       Grouping Operator                       (a+b)/4;

[ ]                       Array access                                  array[3]=2;

->                      Member access from a pointer  ptr->age=20;        Left to right

.                         Member access from an object obj.age=20;

++                     Post-increment                   for( i = 0; i < 10; i++ ) …

—                       Post-decrement                  for( i = 0; i < 10; i– ) …

3      !                         Logical negation                             if( !done ) …

~                        Bitwise compliment                        flags = ~flags;

++                      Pre-increment                       for( i = 0; i < 10; ++i ) …

—                        Pre-decrement                      for( i = 10; i > 0; –i ) …   Right to left

–                         Unary minus                                    int i = -1;

+                        Unary plus                                        int i = +1;

&                        Address of                                  address = &obj;

(type)                 Cast to a given type               int i = (int) floatNum;

sizeof                 Return size in bytes           int size = sizeof(floatNum);

4     ->*                     Member pointer selector              ptr->*var = 24;       Left to right

.*                        Member object selector                obj.*var = 24;

5    *                          Multiplication                                 int i = 2 * 4;

/                           Division                                          float f = 10 / 3;        Left to right

%                         Modulus                                         int rem = 4 % 3;

6   +                          Addition                                          int a=1+2;               Left to right

–                          Subtraction                                    int s=2-1;

7   <<                       Bitwise shift left                             int flags = 33 << 1; Left to right

>>                       Bitwise shift right                           int flags = 33 >> 1;

8  <                       Comparison less-than                    if( i < 42 ) …

<=                     Comparison less-than-or-equal-to  if( i <= 42 ) …       Left to right

>                        Comparison greater-than               if( i > 42 ) …

>=                     Comparison geater-than-or-equal-to if( i >= 42 ) …

9  ==                    Comparison equal-to                        if( i == 42 ) …         Left to right

!=                      Comparison not-equal-to                 if( i != 42 ) …

10 &                      Bitwise AND                                    flags = flags & 42;    Left to right

11 ^                       Bitwise exclusive OR                     flags = flags ^ 42;     Left to right

12 |                        Bitwise inclusive (normal) OR     flags = flags | 42;      Left to right

13 &&                    Logical AND                if( conditionA && conditionB ) … Left to right

14  ||                      Logical OR                   if( conditionA || conditionB ) …    Left to right

15 ? :                    Ternary conditional     (if-then-else) int i = (a > b) ? a : b; Right to left

16 =                     Assignment operator                     int a = b;

+=                   Increment and assign                    a += 3;

-=                   Decrement and assign                  b -= 4;                         Right to left

*=                    Multiply and assign                        a *= 5;

/=                     Divide and assign                          a /= 2;

%=                   Bitwise AND and assign               a %= 3;

&=                    Bitwise AND and assign                 flags &= new_flags;

^=                     Bitwise exclusive OR and assign  flags ^= new_flags;

|=                     Bitwise inclusive (normal) OR and assign flags |= new_flags;

<<=                  Bitwise shift left and assign        flags <<= 2;

>>=                  Bitwise shift right and assign     flags >>= 2;

17 ,              Sequential evaluation operator   for( i = 0, j = 0; i < 10; i++, j++ ) … Left to right

Regards,

Asjad Azeem.

http://www.programmingdoctors.wordpress.com

  1. No trackbacks yet.

Leave a comment