What are Operators in C++ ?

Have you ever felt that how it happens, when you write a simple syntax of a = 5 + 4; on C or C++ you get the output 9. For this entire addition, subtraction, multiplication and division, you don’t need to worry about the internal working process of it. Well, what if I told you that even for +, -, * and /, there is written a separate program and we call them as Operator.

An Operator is defined as the set of basic functionalites like addition, multiplication, subtraction, division, etc, which help in performing specific mathematical and logical computations on the Operands. In other words, we can say that an operator operates the operands. Look into this picture, and you will get the clear understanding.

Operators are defined into 2 categories:

  • Pre-defined Operators
  • User-defined Operators

Pre-defined Operators are those, which have been previously defined or written by programmer and comes with the installed compiler, whereas User-defined Operators is defined by a User and they are basically called as Operator Overloading

Operators are of 4 types:

  1. Arithmatic Operator

These are used in performing mathematical operations like +, – , *, /, ++, –.

These two are also of two types:

  1. Unary Operator: Which works on single operand like +, -, etc
  2. Binary Operator: Which works on two or more operands like ++, –.
  • Relational Operator

   These type of operators are used for comparison of two or more operands. Like  > , < , == , <= , >=. The compare and return the values.

  • Logical Operator

The Logical Operators are used to combine two or more Conditions or Constraints or to complement the evalution in original condition. These are bascially boolean types values and return either True or False to a given condition. For example:

    && is called as AND operator and || is called as OR operator. These are similar to logic gates operation.

 a && b returns true when both a and b are true, else false.

  a || b returns true when either of them is true and false if both of them are false.

  • Bitwise Operator

Bitwise Operator are used to perform bit-level operations on the operands. The operator first converts the operands into Bitwise level or binary representation of number and then performs the logical operations. & is called as bitwise AND and | is called as bitwise OR . For example:

2 & 4, Here 2 is converted into binary number as 0010 and 4 is converted into binary number as 0100. So, 0010 & 0100 gives 0

0010 | 0100 gives  0110 as the output and 6 as the number

  • Assignment Operator 

The assignment Operator is used to assign values to a variable. It is represented  by ‘ = ’. For example, a = 5 + 5 ;  assigns  9 value to a variable.

  ‘ += ‘ or ‘-=’ are also called assignment operators. They first perform the operation and then assign the values. For example,

a += 5   is represented as   “ a = a + 5”

a -= 5   is represented as   “ a = a – 5”

  • Other Operators

     There are some other operators which are also frequently used. These are basically,

  • Ternary Operator or Conditional Operator ( ? ) : It is used to evaluate a condition like if else. For example,   (conditional expression ) ? (option 1) : (option 2)

Option 1 is returned when condition is true else Option 2 is returned when condition is false.

 5 > 9 ? True : False

Answer is False

Sizeof () : Operator is used to give the size or length of the operand. It is unary operand and takes argument as unsigned integral type which is denoted by size_t.

For example, sizeof(8) integer gives 4 bytes. (Depends upon OS also)

Comma Operator (,):  This operator is a binary operator that evaluates first operand and discard the rest result, it then evaluates the second operand and returns the value and it’s type. The Comma Operator act as both operator and separator.

Facebook Comments

Show More

Leave a Reply

Back to top button