C++ Programming & Tutorials for Beginners

C++ Operators

Home  »  C++ Programming  »  C++ Operators

One reason for the power of C++ is its wide range of useful operators. An operator is a function which is applied to values to give a result. You should be familiar with operators such as +,-,/. Arithmetic operators are the most common. Others operators are used for comparison of values, combination of logical states, and manipultation of individual binary digits.


4.1 What is Operators

          As C++ rich in data types, same as C++ is rich in Operator. From every Operator, C++-expression can be defined. By using some Operators we can solve or compute a formula, we can compare two expresssions or variable value, or can create logical relationship between compound statement, can solve a conditinal expresssion, can do low-level programming and special types of Operators.

      Mainly Operators are used to operate two or more than two operands depending upon their processing and given conditions. The relationship of operator with the operands is shown as in the below common statement:

Operand1    Operator    Operand2


Operand1    Operator    Operand2   Operator    Operand3

    Here Operand1, Operands, Operand3 may be variable, a constand or an expression etc. There are mainly eight operators used in C++-Language.

2() [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeidpostfixLeft-to-right
3++ -- ~ ! sizeof new deleteunary (prefix)Right-to-left
* &indirection and reference (pointers)
+ -unary sign operator
4(type)type castingRight-to-left
5.* ->*pointer-to-memberLeft-to-right
6* / %multiplicativeLeft-to-right
7+ -additiveLeft-to-right
8<< >>shiftLeft-to-right
9< > <= >=relationalLeft-to-right
10== !=equalityLeft-to-right
11&bitwise ANDLeft-to-right
12^bitwise XORLeft-to-right
13|bitwise ORLeft-to-right
14&&logical ANDLeft-to-right
15||logical ORLeft-to-right
17= *= /= %= += -= >>= <<= &= ^= |=assignmentRight-to-left

The list of various operators as shown above are described in details as:

Arithmetic Operators

  Arithmetic Operators are used for arithmetic operations like Addition Subraction, Multiplication, Division etc. Mostly arithmetic operators are used in the all the computer languages. There are five arithmetic operators used in C++-language. These all are given in the table below

%modulus (remainder after division)
For example, Suppose a & b are two variables, then arithmetic operators used for different operations as:
a*b(multiplication of a with b)
a/b(division of a by b)
a%b(to find module of a with b)
a+b(addition of a and b)
a-b(subtraction of b from a)

Relational Operators

       These operators are used to create logical relationship between two operands. Relational operators are used for comparison purpose. The expression having two operands and one relational is called Relational Expression. There are mainly six relational operators used in the C++-Language. These all are given in the table as:

<less than
<=less than and equal to
>greater than
>=greater than and equal to
==equal to
!=not equal to
For example, Suppose a & b are two variables, If a=5 and b=2 are two integer type variables then some relational expressions using the relational operations are:

Logical Operators

       Logical operators are used for logical operations. These operations are used for compound relational expressions or logical expressions. When more than one reltional expression occur in a C expression using logical operators, then such type of expressions are called Compound Relational Expressions or Logical expression. These are used in decision-making statement adn some looping statement like if,switch, do while, while and for etc. These statement have either True (1) bracnh or false (0) branch. There are mainly three logical operators used in C++ language as in the table below :

!Logical NOT
(Not process the condition)
||Logical OR
(Process any one of the conditions.)
&&Logical AND
(Process two or more than two relational expressions)

Assignment Operators

       Assignment operators are used for assigning an expression or value (constant) to a variable. Assignment operators are further subdivided into two types:

a) Simple Assignment Operator
b) Short hand Assignment Operator
    Arithmetic Assignment Operator

The general syntax :

v= constant value or variable or expression;

Where v is the variable and expression be any arithmetic expression. for example :
sum = 0;
i = 1
a = b;

Shorthand Assignment
R i+= 1;
p*= a-b;

Conditional Operators

       Conditional Operators are aslo called ? : operators or ternary operator. These operators are used instead of block if statement. The general syntax of conditional operator are as :

exp1 ? exp2 : exp3;

for example

c =(a>b) ? a-b :a + b;

Increment/Decrement Operators:

       These operators are also sometimes called special operators or unary special operators. Another name or Increment/Decrement Operators is Counter Operator. These are two as: ++ (increment operator) and -- (decrement operator). Increment operator are used for incrementing the value one by one. Similarly decrement operator are used for decrementing the value one by one. These are further sub-divided into two categories:

a) Prefix Increment / Decrement Operator
b) Postfix Increment / Decrement Operator

a) Prefix Operator:
       In the Prefix increment operator, first of all value will be increment and the incremented value will be assigned to a variable. Similarly in the prefix decrement operator first of all value will be decrement and then decremented value be assigned to the variabl. The general way is represendted as:


b) Postfix Operator:
       In the postfix increment operator, first of all value will be assigned to a variable and then it will be incremented. Similarly in the postfix decrement operator first of all value will be assigned and then it will be decremented. The general way is represendted as:


Bitwise Operators:

       These are special operators for low level programming. These operators are used for the manipulation of binary data (bits). Ther are six types of Bitwise Operator. The table for Bitwise Operators is as:

&Bitwise AND
| (pipe symbol)Bitwise OR
^Bitwise exclusive OR
<<Bitwise left
>>Bitwise right
~ (tilde)Bitwise NOT (complement operator)

Special Operators:

       These are used for special purposes in C++-language. These operators are used in pointers, structures and unions etc. Some types of special operators are as:

1.Unary Operator
2.Comma Operator
3.Sizeof Operator
4.Type Operator
5.Pointer Operator
6.Member Selection Operator

Next Post »