An Operator is a  symbol that tells compiler to perform certain mathematical or logical operations. Operators are usually same in all programming language like c/c++ , java , python etc.

Built-in C / C++ operators  are :

  • airthmetic operators
  • logical operators
  • bitwise operator
  • relational operators
  • assignment operators
  • misclleneous operators

Arithmetic Operators

Consider the following table

Lets assume A=6, B=4 now

Operator Description Example
+ Adds two operands A + B will give 10
Subtracts second operand from the first A – B will give 2
* Multiplies both operands A * B will give 24
/ Divides numerator by de-numerator A/B will give 1
% Modulus Operator and remainder of after an integer division A%B will give 0
++ Increment operator, increases integer value by one A++ will give 6 BUT ++A will give 7
Decrement operator, decreases integer value by one B– 4 but –B will give 3

when we divide a number , output depends upon our datatype Here ,

  1. if our datatype is integer , then output is rounded off to the nearest low value integer i,e
    here in our case the output should have been 1.5 bt it is rounded of to nearest n low valued integer i.e 1 hence the output is recieved !!
  2. if our datatype is float , now we know that float takes decimal values too
    therfore our output will b 1.5

Now lets talk about increment operator / decrement operator :

here when we write A++ then internally firstly A is printed then it is incremented ( just go with the flow …. i.e from left to right )
i.e why our output is 6 here . In the memory it go incremented later n before getting incremented it got displayed . Also in ++A, here incrementation is done first  according to the symbol , n then its displayed hence output is 7

Logical operators

Assume A=1 and B=0

in terms of boolean 1 means true and 0 means false

Operator Description Example
&& Called Logical AND operator. If both the operands are true, only then condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is true, then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false. !(A && B) is true.

Bitwise operator

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Basically in bitwise operator the operation is performed bit by bit . Assume A=5 B=6

Operator Description Example
& binary AND Operator copies a bit to the result if it exists in both operands. Write A as 0101B as 0110A&B  =        0100 i.e 4
| Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 7 which is 0111
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 3 which is  0011
~ Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. (~A ) will give  1010
<< 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 0101 which is 5 , B<<1 will give 1100
>> 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 5 which is 0101 ,B>>1 will give 0011

Relational operators

Consider A=0, and B=1

Operator Description Example
== Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greaterthan the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Assignment operators

Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C – A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2

Miscellaneous operators 

Operator Description
sizeof sizeof operator returns the size of a variable. For example, sizeof(a), where a is integer, will return 4.
Condition ? X : Y Conditional operator. If Condition is true ? then it returns value X : otherwise value Y
, Comma operator causes a sequence of operations to be performed
. (dot) and -> (arrow) Member operators are used to reference individual members of classes, structures
Cast Casting operators convert one data type to another. For example, int(2.2000) would return 2.
& Pointer operator & returns the address of an variable. For example &a; will give actual address of the variable.
* Pointer operator * is pointer to a variable. For example *var; will pointer to a variable var.

 

Precedence order

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

NOTE:

In binary , ‘0’ means false and ‘1’ means true . Keep learning !!

Have something to add in C Operators ? Share/Ask into comments it in the comments.

Follow us on Facebook, Google Plus and Twitter

Previous                                      C Programming                                      Next