1
Posted January 15, 2013 by Spyros in C/C++ Programming
 
 

An Introduction to the Types of Operators in C++

operator
operator

 

As we all know, C++ is a universal programming language that functions with the help of various constants and variables. Any input that we give in the form of variables or entities are processed, operated and displayed as results by the compiler. But, how does the operation take place? That’s where C++ brings in a concept called the Operators.

What are Operators?

Operators in C++ are symbols that instruct the compiler to carry out a specific logical or mathematical calculation to give the intended result. One of the beneficial features of C++ operators is that they are not based on keywords, but rather on signs and symbols. So, this enhances the comprehension of the topic and eliminates the need to memorize complex keywords to execute a program.

Types of Operators:

C++ language is abundant in built-in operators which are classified under the categories as follows:

Arithmetic Operators:

Arithmetic Operators in C++ allows the programmer to carry out the most basic mathematical manipulations in his program. The types of arithmetic operators in C++ are –

  • +: This operator is used to add two variables or operands
  • – : This operator is used to subtract two operands
  • / : This operator is used to divide two operands
  • *:  This is the multiplication operand, used to find the product of two operands
  • %: Termed as the modulus operator, this operator gives the value of the remainder after a division operation
  • ++: The Increment operator increases the value of the operand by one
  • –: It decreases the operand value by one and is called the decrement operator

Assignment Operator:

The Assignment operator is used to assign a specific value to an entity or variable. Here, the section to the left of the operator is termed as ‘lvalue’ and the section to the right of the operator is termed as ‘rvalue’. The operation or the evaluation in an assignment operator always occurs from right to left, unlike Arithmetic, that occurs from left to right. Some of the commonly used assignment operators are:

  • =: It is the most common operator that is used to assign  a simple value to the variable
  • +=: This operator adds the value of the right variable with the left variable and assigns the value of the result to the left variable. Ex: X+= Y becomes, X= X+Y. This operator similarly performs the same operations with the other arithmetic operators such as subtraction, division, modulus and multiplication.

Logical Operators:

Logical operators in C++ are used to perform Boolean calculations. As we know, Boolean calculations are based on 0 and 1 and the results are usually just true or false. Hence the operators in C++:

  • &&: This operator is used to perform AND operation, where the condition becomes true if both the variables have a non-zero value.
  • ||: This is used to perform OR operator, where the condition is true when either one of the variables have a non-zero value.
  • !: This is the NOT operator and is used to reverse the state of operand. Example – False becomes True.

Relational Operators:

Relational operators are used to check the relativity of two variables and have the following operators.

  • <: This is used to check if the value of the left variable is greater than the value of the right variable. Its opposite sign ‘<’ is used to check if the right variable is greater than the left variable.
  • = =: This checks if the two operands have the same value or not. If yes, the condition is true.
  • !=: This does the same job as ‘==’, but yields a result of true when the values are not equal.
  • >=: This verifies if the variable on the left is greater-than-or-equal to the variable on the right. And the sign ‘<=’ functions the opposite way.

Bitwise Operators:

Bitwise operators are based on truth tables and perform action on every bit.

Miscellaneous Operators:

Some of the commonly used miscellaneous operators in C++ are:

  • Cast: This operator is used to convert the data from one type to another.
  • .->: This operator is used to refer different members of the same structures and classes.
  • * and &: The pointer operators * and & are used to point a value to the variable and return the address of a variable respectively.

So, these were the types of Operators in C++. It is essential that we completely understand the symbols and logical functioning of each, as the further chapters in C++ will entirely revolve around these operators. You can also go through several programs and try to understand where the operators are used and how they are assigned for further comprehension.

This guest post has been brought to you by Robin Mckenzie of www.buycenturylink.com, a site that offers savings and current information on consumers’ broadband internet and cable.


Spyros