In this tutorial you will learn about the C++ Operators and its application with practical example.
C++ Operators
An operator is a special symbol that is used to carry out some specific operation on its operand. In C++, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. Operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.
- C++ Operators
- Type of operators in C++
- C++ Assignment Operators
- C++ Arithmetic Operators
- C++ Increment and Decrement Operators (post and pre)
- C++ Comparison (Relational) Operators
- C++ Logical Operators
- C++ Conditional operator ( ? : )
- C++ Bitwise Operators
- sizeof() Operator
- Comma operator ( , )
- AddressOf ( & )
- Pointer ( * )
- C++ Operators Precedence
- C++ Operator Associativity
Type of operators in C++
In C++, we have following types of operators available –
- Assignment Operators
- Arithmetic Operators
- Increment and Decrement Operators
- Relational Operators
- Logical Operators
- Conditional Operators
- Bitwise Operators
- Special Operators
C++ Assignment Operators
Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression.
Operator | Description | Expression |
---|---|---|
= |
Assignment Operator |
a=b |
+= |
add and assign |
a+=b is equivalent to a=a+b |
-= |
subtract and assign |
a-=b is equivalent to a=a-b |
*= |
multiply and assign |
a*=b is equivalent to a=a*b |
/= |
divide and assign |
a/=b is equivalent to a=a/b |
%= |
mod and assign |
a%=b is equivalent to a=a%b |
<<= |
Left shift AND assign |
a<<=5 is equivalent to a=a<<5 |
>>= |
Right shift AND assign |
a>>=5 is equivalent to a=a>>5 |
&= |
Bitwise AND assign |
a&=5 is equivalent to a=a&5 |
^= |
Bitwise exclusive OR and assign |
a^=5 is equivalent to a=a^5 |
|= |
Bitwise inclusive OR and assign |
a|=5 is equivalent to a=a|5 |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main() { int a= 30; int b= 5; cout<<"W3Adda - C++ Assignment Operators"<<endl; a+=b; cout<<"a+=b :"<<a<<endl; a-=b; cout<<"a-=b :"<<a<<endl; a*=b; cout<<"a*=b :"<<a<<endl; a/=b; cout<<"a/=b :"<<a<<endl; a%=b; cout<<"a\%=b :"<<a<<endl; return 0; } |
Output:-
C++ Arithmetic Operators
Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc. Let variable a holds 20 and variable b holds 10, then −
Operator | Name | Description | Example |
---|---|---|---|
+ |
Addition |
Addition of given operands |
a+b returns 30 |
- |
Subtraction |
Subtraction of second operand from first |
a-b returns 10 |
* |
Multiply |
Multiplication of given operands |
a*b returns 200 |
/ |
Division |
Returns Quotient after division |
a/b returns 2 |
% |
Modulus |
Returns Remainder after division |
a%b returns 0 |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> using namespace std; int main() { int a= 20; int b= 10; cout<<"W3Adda - C++ Arithmetic Operators"<<endl; cout<<"a+b ="<<a+b<<endl; cout<<"a-b ="<<a-b<<endl; cout<<"a/b ="<<a/b<<endl; cout<<"a*b ="<<a*b<<endl; cout<<"a%b ="<<a%b<<endl; return 0; } |
Output:-
C++ Increment and Decrement Operators (post and pre)
In C ++, ++ and — are know as increment and decrement operators respectively. These are unary operators it means they works on single operand. ++ adds 1 to operand and — subtracts 1 to operand respectively. When ++ is used as prefix(like: ++i), ++i will increment the value of i and then return it but, if ++ is used as postfix(like: i++), operator will return the value of operand first and then only increment it.
Operator | Example | Description |
---|---|---|
++ [prefix] | ++a | The value of a after increment |
++ [postfix] | a++ | The value of a before increment |
— [prefix] | –a | The value of a after decrement |
— [postfix] | a– | The value of a before decrement |
C++ Comparison (Relational) Operators
Comparison Operators are used evaluate a comparison between two operands. The result of a comparison operation is a Boolean value that can only be true or false. Comparison Operators are also referred as relational operators.
Let variable a holds 20 and variable b holds 10, then −
Operator | Description | Example |
---|---|---|
> |
greater than |
a>b returns TRUE |
< |
Less than |
a<b returns FALSE |
>= |
greater than or equal to |
a>=b returns TRUE |
<= |
less than or equal to |
a<=b returns FALSE |
== |
is equal to |
a==b returns FALSE |
!= |
not equal to |
a!=b returns TRUE |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main() { int a= 20; int b= 10; cout<<"W3Adda - C++ Relational Operators"<<endl; if(a > b) { cout<<"a is greater than b."<<endl; }else{ cout<<"b is greater than a."<<endl; } return 0; } |
Output:-
C++ Logical Operators
Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false. Let variable a holds true or 1 and variable b holds false or 0, then −
Operator | Name | Description | Example |
---|---|---|---|
&& |
Logical AND |
return true if all expression are true |
(a && b) returns false |
|| |
Logical OR |
return true if any expression is true |
(a || b) returns true |
! |
Logical NOT |
return complement of expression |
!a returns false |
C++ Conditional operator ( ? : )
In C++, conditional operator is considered as short hand for if-else statement. Conditional operator is also called as “Ternary Operator”.
Syntax:
1 |
condition ? result1 : result2 |
If condition is true the expression will return result1, if it is not it will return result2.
Example:-
1 2 |
result = (10 > 15) ? "Greater" : "Smaller"; cout<<result; |
Output:-
1 |
Smaller |
C++ Bitwise Operators
Bitwise operator are used to perform bit level operation over its operand.
Let A = 60; and B = 13;
Binary equivalent
A = 0011 1100
B = 0000 1101
Operator | Meaning | Example | Description |
---|---|---|---|
& | Binary AND | (A & B) | It returns 12 which is 0000 1100 |
| | Binary OR | (A | B) | It returns 12 which is 0000 1100 |
^ | Binary XOR | (A ^ B) | It returns 49 which is 0011 0001 |
~ | Ones Complement | (~A ) | It returns -60 which is 1100 0011 |
<< | shift left | A << 2 | It returns 240 which is 1111 0000 |
>> | shift right | A >> 2 | It returns 15 which is 0000 1111 |
sizeof() Operator
It returns the size of its operand’s data type in bytes.
Example:
1 |
a = sizeof (char); |
This will assign the value 1 to a because char is a one-byte long data type.
Comma operator ( , )
The comma operator (,) allows us to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.
For example, the following code:
1 |
i = (j=3, j+2); |
- first assign the value 3 to j
- assign j+2 to variable i.
At the end, variable i would contain the value 5 while variable j would contain value 3.
AddressOf ( & )
Returns the address of an variable.
Example:
1 2 |
&a; //Will give actual address of the variable. |
Pointer ( * )
Pointer to a variable.
Example:
1 2 |
*a; //Will pointer to a variable. |
C++ Operators Precedence
Operator precedence defines the order in which given mathematical expression is evaluated. When an expression includes multiple operators then each of the single part of given expression is evaluated in a certain order following some rules defined as per operator precedence. Operator with higher precedence is evaluated first and operator with lowest precedence is evaluated at last.
C++ Operator Associativity
Operators with same precedence follows operator associativity defined for its operator group. In C++, operators can either follow left-associative, right-associative or have no associativity. Operators with left-associative are evaluated from the left to right, operators with right-associative are evaluated from right to the left and operators with no associativity, does not follow any predefined order.
Following is C++ operator precedence table with their respective associativity –
Category | Operator | Associativity |
---|---|---|
Postfix | () [] -> . ++ – – | Left to right |
Unary | + – ! ~ ++ – – (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + – | Left to right |
Shift | << >> | Left to right |
Relational | < <= > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |