Showing posts with label c language operators. Show all posts
Showing posts with label c language operators. Show all posts

Saturday 28 September 2013

Operators And Their Precedence In C++

Operators:

Precedence of operators in c++ is the most basic thing used in computer for calculations,these calculations can be done by use of several arithmetic operators. So, we are going to discuss arithmetic operators in this c tutorial for beginners. There are five arithmetic operators in C++. Those c++ operators are listed below:

  1. +  (Addition)
  2. –  (Subtraction)
  3. *  (Multiplication)
  4. /  (Division)
  5. %  (Mod, modulus or remainder)


You can use these operator in both integral and floating number data types except modulus operator. As modulus operator is used in integral data type to find the remainder. 


Let’s assume an expression:

x + 2 * 5 + 6 / y

x and y are unknown variables. This arithmetic expression is formed by using operators and numbers. The numbers in the expression are called operands. 


C++ has two type of operators:

Unary operator: An operator that has only one operands.
Binary operator: An operator that has two operands.

Expression1: 3+4
In this expression, ‘+’ is a binary operator as ‘+’ has two operands.
Expression2: -3, +4
In this expression, ‘-‘ and ‘+’ are unary operators as they have only one operand. The ‘-‘ states that 3 is a negative number and ‘+’ indicates that 4 is a positive number. 


Examples:

Expression Result

  2 + 10 =  12
21 + 89  = 110
30 -  2010
30 -  90  =  -60
2   *  20 =  40
5   /   2  =   2
34 % 5  =   4
4  &   6  =   4


Note:

You should be careful in taking mod of numbers. For example, 22 % 5 = 2 and in -22 % 5 = -2. So, you should be careful when taking mods as there is a difference when signs with the number change.


Associativity And Precedence Of Operators In C:

When more than one operator is used in arithmetic expression, C++ follows the rules of precedence of arithmetic operators in c++ to evaluate the expression. 
Operator precedence in C++:
  1.  * , / , %
  2. + , -
 
Final words:
In this c tutorial for beginners of shapes in c we discussed different operators used in programming , there use and the precedence of operators in c++