What is operator ?

Operators are the symbols or the instructions that is used to tell the compiler for which type of operation to be performed in the program. The operators are used for mathemetical and logical manipulations.

Types of operators in C++ are :

  • Arithmetic Operators
  • Assignment Operators
  • Increment and Decrement Operator
  • Relational Operators

  • Logical Operators
  • Conditional operator

  • Sizeof Operator
  • Bitwise Operators
  • Other Operators

1. Arithmetic operators :

    The arithmetic operators are used to perform numeric calculations on the data and the variable.

Arithmetic operators are of following types :

arithmetic operators


a) Addition : It is used to do addition between two or more operands in a program. The addition is denoted using the ' + ' sign.

     For example : a + b 

b) Substraction: It is used to do substraction between two or more operands in a program. The substraction is denoted using the ' - ' sign.

     For example : a - b 


c) Multiplication :
It is used to do multiplication between two or more operands in a program. The multiplication is denoted using the ' * ' sign.


     For example : a * b 


d) Division : It is used to do division between two operands in a program. The division is denoted using the ' / ' sign.


     For example : a / b 


e) Modulus : It is used to compute the remainder of the given operands in a program. The modulus is denoted using the ' % ' sign.


     For example : a % b 


arithmetic operator




2. Assignment operators :


    The assignment operator is used to assign any value to a variable in a program. It is denoted using the ' = ' sign.
assignment operators



      For example : a = 2 
                Here 2 is assigned to the variable a.


assignment operators


3. Increment and Decrement operator :


    In C++ we have to useful Operators ( ++ ) and (  -- ). The ( ++ ) operator is known as increment operator, which increases the value of the variable by 1. The ( -- ) operator is known as decrement operator, which decreases the value of the variable by 1. These both operators are unary operators because they work on single variable.

    For example :  a++ ( It will give result as a+1) 
                            a-- ( It will give result as a-1)

4. Relational Operators:


    The Relational Operators are used to compare two variables or quantities and to take a decision depending on their relation.

If the relation is true it returns 1.
If the relation is false it returns 0.

relational operators



For example : 4 > 6 ( It will return 0, as the given statement is false)
                       8 < 10 ( It will return 1, as the given statement is true).



relational operators




5. Logical Operators :

    The Logical Operators are used for testing or checking more then one condition and making the decisions according to it.

It has three operators :
  • && - It is the condition in which both the condition should satisfy, if in case one condition is also false then it will return 0.

  • | | - It is known as OR operator. In this condition, at least one condition should satisfy otherwise it will return 0.

  • != -  It is known as NOT operator. In this case, if the condition is false it will return 0. 

6. Conditional operator :

     It is also known as the ternary operator because it works on three expression as operands. The first expression is the given condition, which will be tested and if the condition is true then the second expression is executed or else the third condition will be executed.


For example : 2 > 4 ? 1 ; 0  ( It will return 0)

7. Sizeof operator : 


The sizeof operator is used to know the size of any datatype. The operator is used using ( sizeof (datatype) ) syntax.

For example : sizeof( int )
                       sizeof( char )

8. Bitwise operators :

     The bitwise operators work on bit level. It is applied only for the integers.
Bitwise operator



  

    C++ tutorial 


    Datatypes in C++