What are Operators in C++? Types | Syntax | Implementation

Operators in C++

Have you ever felt 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.

Well, what if I told you that even for +, -, * and /, there is written a separate program and we call them Operator.

An Operator is defined as the set of basic functionalities like addition, multiplication, subtraction, division, etc., which help perform 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 a clear understanding.

what are operators in c++? types | syntax | implementation

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 the programmer and come with the installed compiler, whereas a User defines User-defined Operators and they are basically called 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 combine two or more Conditions or Constraints or complement the evaluation in original condition. These are 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 is true and false if both are false.

  • Bitwise Operator

Bitwise operators are used to performing bit-level operations on the operands. The operator first converts the operands into Bitwise level or binary representation of numbers 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 a condition is true, else Option 2 is returned when the condition is false.

 5 > 9 ? True : False

Answer is False

Sizeof (): Operator is used to giving the operand’s size or length. It is a unary operand and takes an argument as an unsigned integral type, 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 the first operand and discards the rest result. It then evaluates the second operand and returns the value and its type. The Comma Operator acts as both operator and separator.

Leave a Reply

Back to top button