Arithmetic Operators




Arithmetic Operator:   There are two types of Arithmetic Operator - 

  1. Unary Arithmetic Operator
  2. Binary Arithmetic Operator
Unary Arithmetic Operator: The operator that works on a single operand is known as UNARY ARITHMETIC OPERATOR.

       For Example:-   +a, -a  represent  ' + ' &  ' - ' and hence, are the Unary Arithmetic Operator. 

Binary Arithmetic Operator:  The arithmetic operator which works on two operands is known as Binary Arithmetic Operator.
     There are six type of Binary Operators:-

Operator Name                                   Symbol                                                 Working
Plus                                                        +                                  Add two numeric values
Minus                                                      -                                  Subtract two numeric values
Slash                                                      /                                  Divide two numeric values and return quotient 

Asterisk                                                  *                                  Multiply two numeric values and return product

Mod                                                       %                                 Divide two numeric values and return reminder 





Relational Operator: There are six type of relational operators -

Operator Name                                   Symbol                                                 Working
Is Equal                                                 ==                                 Checks that two values are equal or not
Less Than                                              <                                   Checks that first value is smaller than or not                                
                                                                                                  to the second value.
Greater Than                                          >                                   Checks that first value is greater than or not 
                                                                                                  to the second value
Less Than Or Equal To                           <=                                  Checks that first value is smaller than or 
                                                                                                  equal to second value or not.
Greater Than Equal To                            >=                                 Checks that first value is greater than or 
                                                                                                  equal to second value or not.
Not Equal To                                          !=                                   Checks that first value is not equal to 
                                                                                                  second value or not.

The relational operators are used to check condition and always return a Boolean value (true/false) according to condition. for example a==b read as " is 'a' equal to 'b',  if the answer of the question is in the form of yes then it will return true other wise it will return false.

We can also understand relational operator by the examples given bellow-: 

      Let a=5, b=6 and c=5

Conditions                                                  Answer                                                Results

a==b (Is a equal b)                                           yes                                                     true
a==c (Is a equal c)                                           yes                                                     false
b==c (Is b equal c)                                           no                                                       true  
a<b (Is a  less than b)                                      yes                                                     true
b<c (Is b  less than c)                                      no                                                       false
a>b (Is a  greater than b)                                  no                                                       false
b>c  (Is b  greater than c)                                 yes                                                     true
a<=b (Is a less than or equal to b)                     yes                                                     true
a<=c (Is a less than or equal to c)                     yes                                                     true
b<=a (Is b less than or equal to a)                     no                                                       false
b<=c (Is b less than or equal to c)                     no                                                       false
a>=b (Is a greater than or equal to b)                 no                                                       false
a>=c (Is a greater than or equal to c)                 yes                                                     true
b>=a (Is b greater than or equal to a)                 yes                                                     true
b>=c (Is b greater than or equal to c)                 yes                                                     true
a!=b (Is a not equal to b)                                   yes                                                     true
b!=c (Is b not equal to c)                                   yes                                                     true
c!=a (Is c not equal to a)                                   no                                                       false
a==a (is a equal to a)                                       yes                                                     true                                           
b<b (is b smaller than b)                                   no                                                       false
etc...................




Logical Or Boolean Operator:

Logical operator are basically three type - 

&&   (AND)

|| (OR)

NOT (!)

AND, OR operator are use to check two conditions simultaneously whether Not operator works only on single condition. We can understand these operators by the following truth table:


"AND" Gate Truth Table
Condition1 Condition2 Result(&&)
True True True
True False False
False True False
False False False


"OR" Gate Truth Table
Condition1 Condition2 Result(||)
True True True
True False True
False True True
False False False


"NOT" Gate Truth Table
Condition Result(!)
True False
False True


Increment / Decrement Operator
Increment and decrement operator are use to increase or decrease value by 1 in a variable.




In c programming language increment decrement operator evaluate from right to left. We can understand the working of increment and decrement operator by the following example:
Let a=1
and we want to calculate :
b=a++ + ++a;
so first taking the right a and with the right 'a' pre -  increment operator is associated so first we have to increase the value of a and then use. in the way in the place of right 'a' we can put 2 as
b=a++ + 2
and because the left 'a' having post increment operator so first we can use the value then change. So the expression will be:
b=2+2=4.
In this way we can derive a formula that for the pre -  increment operator first we have to change the value(increase/decrease value by 1) then use and in post increment and decrement operator first we have to use the value then change(increase or decrease by 1)


Now solve some exercise:
Let a=1, b=2
 1. c=a++ + b++;
 2. c=++a + a++ + --a + a--;
 3. c=a + ++a + --b + a++;
 4. c=++b + a++ * --a + a++;
 5. c= b++ + ++b - ++a + a--;


Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows:
pqp & qp | qp ^ q
00000
01011
11110
10011
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A  = 1100 0011
The Bitwise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:
OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands.(A & B) will give 12, which is 0000 1100
|Binary OR Operator copies a bit if it exists in either operand.(A | B) will give 61, which is 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) will give 49, which is 0011 0001
~Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.(~A ) will give -61, which is 1100 0011 in 2's complement form.
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 will give 240 which is 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 will give 15 which is 0000 1111



Misc Operators ↦ sizeof & ternary

There are few other important operators including sizeof and ? : supported by C Language.
OperatorDescriptionExample
sizeof()Returns the size of an variable.sizeof(a), where a is integer, will return 4.
&Returns the address of an variable.&a; will give actual address of the variable.
*Pointer to a variable.*a; will pointer to a variable.
? :Conditional ExpressionIf Condition is true ? Then value X : Otherwise value Y

Operators Precedence in C

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.
For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Category Operator Associativity 
Postfix () [] -> . ++ - -  Left to right 
Unary + - ! ~ ++ - - (type)* & sizeof Right to left 
Multiplicative  * / % Left to right 
Additive  + - Left to right 
Shift  << >> Left to right 
Relational  < <= > >= Left to right 
Equality  == != Left to right 
Bitwise AND Left to right 
Bitwise XOR Left to right 
Bitwise OR Left to right 
Logical AND && Left to right 
Logical OR || Left to right 
Conditional ?: Right to left 
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left 
Comma Left to right
Avinash Singh

My name is Avinash Singh. I am a Computer Teacher cum Software Developer. For more detail you can visit on my company web site: http://e-visiontechnocraft.co.in

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post