More on Operators (V)


The following topics are covered in this section:


SizeOf Operator (sizeof)

This is again another unary operator. Just as the name implies, this operator returns the length (in bytes) of the variable that is mentioned in the parentheses. For example the size of a character is 1 (i.e. 1 byte).

      // Using sizeof operator
      #include <iostream.h>
      int main ( )
      {
          int a;
          cout<<sizeof (a);         // Since a is an integer, the output will be 4
          cout<<sizeof(char);     // Output will be 1
          return 0;
      }

It is not a must to mention a variable within the parentheses; you could also mention one of the in-built data types. Some operating systems allocate a different memory space for data types and it is better to use the ‘sizeof’ operator to determine the size of data types instead of assuming that all systems use the same number of bytes.


Conditional Operator (?:)

The conditional operator is a ternary operator (i.e. it operates on 3 operands at a time). We’ll take a closer look at it in the next chapter. For the time being just remember that it needs three operands and it is denoted by:

? :


Operator Precedence

When a statement has more than one operator, the operator precedence determines as to which operator is given priority. For example if you have a * and a +, the compiler has to decide whether it will multiply and add or add and multiply. This will depend on the operator precedence.

The parentheses have the highest precedence. For instance if you write:

        (5+3)*5

the result will be (8)*5 or 40.

If you write the same without using parentheses as

        5+3*5

the result will be 20 (because * has a greater priority than +). The order for operator precedence is listed below (starting from the highest priority):

Parentheses ( )
Unary Operators !
Unary minus -
Multiplication *
Division /
Modulo %
Addition +
Subtraction -
Relational Operators <
      <=
      >
      >=
Equality operator ==
Inequality operator !=
Logical Operator &&
     ||
Conditional operator ?:
Assignment =
Arithmetic assignment *=, /= , %= , += , -=

Beware: It is better to write long expressions using parentheses otherwise it could lead to a lot of confusion and also to potential logical errors.


Associativity

If two operators have a different priority level then their execution order depends on the operator precedence. What will happen if two operators have the same priority level?

When 2 operators in an expression have the same priority, the expression is evaluated using their associativity. Consider the statement:

net = basic + allowance – tax;

Both + and – have the same priority level. Almost all of the operators except the assignment (and arithmetic assignment) operators have associativity from left to right. The assignment operator has associativity from right to left. Since the + and – binary operators have an associativity of left to right the expression for ‘net’ is the same as:

net = (basic + allowance) – tax;

When you perform multiple assignments:

x = y = 0;

the associativity of the assignment operator (=) is taken into account and thus the order of evaluation will be:

y = 0 (which will give ‘y’ a value of 0) followed by x = y (which will assign 0 to ‘x’).


Comma Operator

The comma operator can accept two expressions on either side of the comma. When executed, the left side expression is first evaluated followed by the right side expression. Ultimately it is the expression on the right side that will be the value of the entire expression.

First the ‘x’ will be assigned 1 and then ‘y’ is assigned a value of 5. The comma operator is equivalent to saying "do this task and do this also". In this case the compiler will do:

x = 1 and then y = 5

The rightmost expression is

y = 5

and hence the value of the entire expression

(x=1,y=5)

is 5. Be careful while assigning the value of a comma separated expression to a variable. The comma operator has lower operator precedence than the assignment operator. If we type:

        y = (x = 1 , x = 5 , x + 10);

x will have a value of 5 while y will be 15. If we type:

        y = x = 1, x = 5 , x+10;

the value of ‘y’ will be 1 and that of ‘x’ will be 5. This is because the compiler will perform the following operations:

y = x = 1;         // y and x are set to 1

x = 5;             //x value is 5

x + 10;         //the value of the entire comma separated expression is 15

The comma operator will be used usually in ‘for’ loops.


Bitwise Operator:

The bitwise operators available in C++ are tabulated below:

Bitwise Operator Symbol

Function

&

AND individual bits

|

OR individual bits

^

EXOR individual bits

~

NOT of individual bits (or complement operator)

>>

Right-shift operator

<<

Left-shift operator

These operators will operate on individual bits of the operand (i.e. on the binary representation of data). These operators are dealt with in detail in Chapter 13.

Remember: Do not confuse the && and & operators or the || and | operators (logical operators are entirely different from bitwise operators).


Recap


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.