Add to Technorati Favorites
Google

Operators in C++

Like C or any other programming language, C++ also has operators. C++ operators include all operators of 'C' and many others. In this post, we will see what operators are present in C++ along with their rules of associativity and preceedence.

C++ operators can be classified into following categories:


1. Arithmetic Operators
  • Unary Minus (-n)
  • Unary Plus (+n)
  • Postfix Increment (n++)
  • Prefix Increment (++n)
  • Postfix Decrement (n--)
  • Postfix Increment (--n)
  • Addition (x+y)
  • Substraction (x-y)
  • Multiplication (x*y)
  • Division (x/y)
  • Modulus (x%y)

Binary operators +,-,*,/,% can be clubbed with "=" to form respective 'Assignment By' operators. For example,
x+=y is same as x = x+y and referred as 'Assignment by Addition' or simply x plus equals y

2. Comparisons Operators
  • Less Than (a
  • Less Than or Equal To (a<=b)
  • Greater Than (a>b)
  • Greater Than or Equal To (a>=b)
  • Not Equal To (a!=b)
  • Equal To (a==b)

3. Logical Operators
  • Negation (!a)
  • Logical AND (a&&b)
  • Logical OR (a||b)

4. Bitwise Operators
  • Left Shift (a<
  • Right Shift (a>>b)
  • Bitwise AND (a&b)
  • Bitwise OR (a|b)
  • XOR (a^b)
  • Bitwise Compliment (~)

Like arithmetic operators, <<,>>,&,|,^ can be clubbed with "=" to form respective 'Assignment By' operator like x<<=y is same as x = x<<y

5. Miscellaneous

  • Assignment Operator (p=q)
  • Address Of (&p)
  • Indirection (*p)
  • Function call ( fun() )
  • Subscript ( a[1])
  • Member (s.e)
  • Member by Pointer (p->e)



  • Member by Pointer Function Pointer Indirection (p->*e)
  • Member by Pointer Indirection (p.*e)
  • Cast (type(t))
  • Comma (a,b)
  • Ternary Conditional (n=x?y:z)
  • Scope Resolution (::)
  • Member Function Pointer (a::*b)
  • Size of (sizeof(var), sizeof(data-type))
  • Type information (typeid(var), typeid(data-type))
  • Memory Allocation (new data-type, new data-type[])
  • Memory Deallocation (delete var, delete [] var)

Rules of Associativity
All operators have left-to-right associativity except following which have right-to-left associativity:
  • Prefix Increment and Decrement (++,--)
  • Unary Plus and Minus (+,-)
  • Logical and Bitwise NOT (!, ~)
  • Address of (&)
  • Indirection (*)
  • Sizeof (sizeof(a))
  • Type casting (type())
  • new and delete for arrays

Rules of Precendence
  • Scope resolution has highest precedence
  • Comma operator (,) has lowest precedence
  • Unary operators have precedence over binary operators
Ending Remarks
  • There are 17 levels in precendence hierarchy. Interested readers can refer to a book, otherwise above rules are enough to know.
  • You may have also noted that all operators are symbolic except new, delete, sizeof, and typeid.
  • Use paranthesis wherever operators associativity and precedence appears to be confusing
  • Sometimes, operator precedence and associativity vary across compilers
  • C++ also support operator synonyms like xor_eq (^=)
  • Also, most of these operators can be overloaded except few. We will see operator overloading in later posts.

No comments: