Description:
C/C++ operators In this tutorial, we learn what is operators and how to use these operators in c/c++ programming with a full explanation.
Arithmetic operators in C / C++:
The arithmetic operators are the symbols that represent arithmetic operations. These are used in arithmetic expressions. Each arithmetic operators operates upon two numeric values( constants and variables ) and returns a value.
The following arithmetic operators are used in c/c++:
Operators  Meaning 
+ 
Addition 
_ 
Subtraction 
* 
Multiplication 
/ 
Division 
% 
For reminder 
The ‘ + ’ and ‘ – ‘ symbols are quite familiar to you. The ‘ + ’ symbol indicates addition. Whereas ‘ – ’ symbol indicates subtraction.
The ‘ * ’ (asterisk ) is used in c/ c++ to indicate multiplication.
The ‘ / ’ (slash) is used to indicate both real and integer division. If both operands in division operators are integers the result will be an integer. For example:
5/3 —–>1
3/5 —–>0
Whereas on the other hand real division is performed producing the real result if both or either of the operand is real for example:
5.0 / 2.0 —–> 2.5
5 / 3.0 ——–> 1.666667
3.0 / 4 ——> 0.75
In the division operator, the second operand must be non zero.
The percent sign ( % ) produces a modulus or a reminder of an integer division. % computes the arithmetic remainder after a first integer value is divided by a second integer value. for modulus operator, both operands must be an integer and the second operand must be non zero otherwise it will not work.
2 % 2 ——> 0
3 % 2 ——> 1
11 % 3—–> 2
2 % 10 —> 2
5 % 10 —> 5
All arithmetic operators except the remainder operator are used for all types of numeric data.
Example how to use arithmetic operators in c/c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
#include <iostream> using namespace std; int main() { int a,b,c,d,e; a= 1+2; b= 105; c= 6*2; d= 4/2; e= 5%2; cout<<"addition of 1 and 2 is :"<<a<<endl; cout<<"subtraction of 10 and 5 is :"<<b<<endl; cout<<"multiplication of 6 and 2 is :"<<c<<endl; cout<<"division of 4 and 2 is :"<<d<<endl; cout<<"remainder of 5 and 2 is :"<<e<<endl; return 0; } Output: addition of 1 and 2 is :3 subtraction of 10 and 5 is :5 multiplication of 6 and 2 is :12 division of 4 and 2 is :2 remainder of 5 and 2 is :1 
Increment ( ++ ) and Decrement ( — ) c/c++ operators:
C / C++ supports the ++ and – operators to increment and decrement variables by on respectively. The increment operator ( ++ ) is used to add 1 to variable and decrement operator ( — ) is used to subtract 1 from variable. These operators can use either side of the variable. So let’s discuss these operators in detail.
Increment ( ++ ) operator:
The increment operator is represented by a double plus (++) sign. It is used to add 1 to the value of an integer variable. This operator can be used before or after the variable name.
For example:
If we want to add 1 to a value of variable ab it is normally written as
ab = ab+1;
by using the increment operator “ ++ ” it is written as
ab++;
the increment operator can be written either before or after the variable. If it is written before the variable it is known as prefixing. If it is written after the variable it is known as postfixing. Prefix and postfix operators have a different effects when they are used in expression.
Prefix increment operator:
When an increment operator is used in prefix mode in an expression it adds 1 to the value of the variable before the value of the variable is used in the expression.
Example how to use prefix increment operators in c++ programming:
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, b, c , sum; a=1; b=1; c=3; sum= a+b+c+(++c); cout<<"sum = "<<sum<<endl; cout<<"c = "<<c; return 0; } Output: sum = 9 c = 4 
In the above program, 1 will be added to the value of c before it is used in the expression thus after execution the sum will be equal to 9 and the value of c will be 4.
Postfix increment operator:
When an increment operator is used in postfix mode in an expression it adds 1 to the value of the variable after the value of the variable has been used in the expression. For example if in the above example increment operator is used in postfix mode the result will be different the statement will be as shown below:
Sum= a + b + c + c++;
In this case, 1 will be added to the value f c after its existing value has been used in the expression. Thus after execution, the sum will be equal to 5 and the value of c will be 4
Decrement ( — ) operators:
The decrement operator is represented by double minus ( — ) sign it is used to subtract 1 from the value of an integer variable.
For example, to subtract 1 from the value of a variable ab the decrement statement is written as
Ab–;
Or
ab;
Prefix decrement operator:
When a decrement operator is used in prefix mode in an expression it subtracts 1 from the value of the variable before the value of the variable is used in the expression.
Example how to use prefix decrement operators in c++ programming:
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, b, c , sum; a=1; b=1; c=3; sum= a+b+c+(c); cout<<"sum = "<<sum<<endl; cout<<"c = "<<c; return 0; } Output: sum = 7 c = 2 
In the above program, 1 will be subtracted from the value of c before it is used in the expression thus after execution the sum will be equal to 7 and the value of c will be 2.
Postfix decrement operator:
When a decrement operator is used in postfix mode in an expression it subract 1 from the value of the variable after the value of the variable has been used in the expression. For example if in the above example decrement operator is used in postfix mode the result will be different the statement will be as shown below:
Sum= a + b + c + c–;
In this case, 1 will be subtracted from the value f c after its existing value has been used in the expression. Thus after execution, the sum will be equal to 5 and the value of c will be 2.
Assignment operators ( = ):
The assignment operator ( = ) is used to assign value to variables. It is used to assign a numeric or character value to a variable. However, string data can ot be assigned to a variable with this operator.
The assignment operator evaluates an expression and then assigns its value to a variable. The assignment operator ( = ) is used to assign the calculated value to a variable. The expression is written on the right side of the assignment operator. The variable to which the value of the expression is to be assigned is written on the left side of the assignment operator.
The syntax of the assignment operator is
Var= expression;
Where:
Var:
The variable name to which the value of the expression is assigned. The variable type must be the same as the type of value returned by the expression.
Expression:
An expression may be a combination of operands (variable and constants ) and arithmetic operators.
A character can also be assigned but a string cannot be assigned to a string variable.
Example how to use assignment operators in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
#include <iostream> using namespace std; int main() { int year, month; year=20; month=year*12; cout << "year =" <<year<< endl; cout << "month =" <<month<< endl; return 0; } Output: year =20 month =240 
Relational c++ operators:
Relational operators are used to form conditions. These are used to write a conditional expression that describes the relationship between two values .these operations allow us to compare two values to see whether they are equal to each other unequal or whether one is greater than the other or vice versa. Relational operators return the 1 (true) or 0 (False). Don’t confuse the assignment operator ( = ) with the equals relational operator (==) the six relational operators in c++ are:
Greater than ( > ) :
It is used to test if one expression is greater than the other expression. For example, if exp1 and exp2 are two expression then
exp1 > exp2
if exp1 greater than exp2 then this expression returns a true value. if exp1 is less than exp2 then this expression returns a false value.
Greater than or equal to ( > =) :
It is used to test if one expression is greater than or equal to the other expression. For example, if exp1 and exp2 are two expressions then
exp1 >= exp2
if exp1 greater than or equal to exp2 then this expression returns a true value. if exp1 is less than exp2 then this expression returns a false value.
less than ( < ) :
It is used to test if one expression is less than the other expression. For example, if exp1 and exp2 are two expressions then
exp1 < exp2
if exp1 less than exp2 then this expression returns a true value. if exp1 is greater than exp2 then this expression returns a false value.
less than or equal to ( < =) :
It is used to test if one expression is less than or equal to the other expression. For example, if exp1 and exp2 are two expression then
exp1 <= exp2
if exp1 less than or equal to exp2 then this expression returns true value. if exp1 is greater than exp2 then this expression returns a false value.
Equal to ( ==) :
It is used to test if one expression is equal to the other expression. For example if exp1 and exp2 are two expression then
exp1 == exp2
if exp1 equal to exp2 then this expression returns true value. if exp1 is not equal to (less than or greater than ) exp2 then this expression returns false value.
not Equal to ( !=) :
It is used to test if one expression is not equal to the other expression. For example if exp1 and exp2 are two expression then
exp1 != exp2
if exp1 is not equal to (less than or greater than) exp2 then this expression return true value. if exp1 is equal to exp2 then this expression returns false value.
Example how to use Relational operators greater than ( > ) and less than or equal to (<=) in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
#include <iostream> using namespace std; int main() { float current_units, previous_units, units,bill; cout<<"Enter the current reading : "; cin>>current_units; cout<<"enter the previous readig : "; cin>>previous_units; units= current_units  previous_units; if (units > 300) { bill= units*3.5 + units*0.05/100.0; cout<<"Electricity bill is = "<<bill; } if(units <= 300) { bill= units*3.5 + units*0.05/100.0; cout<<"Electricity bill is= "<<bill; } return 0; } Output: Enter the current reading : 42342 Enter the previous reading : 2323 Electricity bill is = 140087 
Example how to use Relational operators equal to (==) in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 
using namespace std; int main() { int n; cout<<"Enter a number : "; cin>>n; if(n%3==0) { cout<<"The number "<<n<<" is divisible by 3 "; } else { cout<<"The number "<<n<<" is not divisible by 3 "; } return 0; } Output: Enter a number : 4 The number 4 is not divisible by 3 Output: Enter a number : 9 The number 9 is divisible by 3 
Example how to use Relational operators not equal to (!=) in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
#include <iostream> using namespace std; int main() { int n; cout<<"Enter a number : "; cin>>n; if(n != 10) { cout<<"The number "<<n<<" is not equal to 10 "; } else { cout<<"The number "<<n<<" is eqaual to 10 "; } return 0; } Output: Enter a number : 5 The number 5 is not equal to 10 Output: Enter a number : 10 The number 10 is not equal to 10 
Logical C++ Operators:
More than one condition can be combined and tested in an if statement by connecting them with logical operator &&(AND), (OR), and ! (NOT). Such condition expression are referred to as Boolean expression complex or compound expression.
A compound expression with && logical expression is true of both of the relational expression are true otherwise it is false for examples:
If((marks > 70) && (marks < 80))
Cout<<”garde is A”;
Suppose if marks is 75 the expression ((marks >= 70) && (marks < 80)) is true if marks has the value 60 the expression ((marks >= 70) && (marks < 80)) is false.
The compound expression with () logical operation is true if any of the conditions is/are true and false only in the one case when all condition are false. For example:
If((choice == ‘Y’)  (choice == ‘y’))
Goto top;
Thus the logical expression ((choice == ‘Y’)  (choice == ‘y’)) is true if either of the relational expressions is true otherwise it is false.
These definitions are summarized in the following truth table which displays app possible values for two decision making condition X and Y and the corresponding values of the logical expression.
X

Y

(X&&Y)

(XY)

true  true  true  True 
true  False  False  True 
False  True  False  True 
False  False  False  False 
now we discuss each of them in detail. There are three type of logical operators in c++ which are:
 &&—–>AND Operator
  —> OR Operator
 !——> NOT Operator
The && (AND) Operator in c++:
The && (AND ) operator is used to combine two or more relational expressions. If all relational expression are true then the output returned by the compound expression is true. If anyone relational expression in the compound expression is false the output is false.
Example how to use logical operators && (AND) in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 
#include <iostream> using namespace std; int main() { int a,b,c; cout<<"Enter 1st number : "; cin>>a; cout<<"Enter 2nd number : "; cin>>b; cout<<"Enter 3rd number : "; cin>>c; if ((a>b)&& (a>c)) cout<<"1st value is greater"; else if((b>a)&& (b>c)) cout<<"2nd value is greater"; else cout<<"3rd value is greater"; return 0; } Output: Enter 1st number : 4 Enter 2nd number : 2 Enter 3rd number : 1 1st value is greater 
The  (OR) Operator:
The  (OR) operator is used to combine more than one relational expression. if anyone of the given relational expressions is true the output will be true otherwise the output will be false.
Example how to use logical operators  (OR) in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 
#include <iostream> using namespace std; int main() { int a; char op; cout<<"Enter 1st number : "; cin>>a; cout<<"Enter any character : "; cin>>op; if((a>0) (op=='y')(op=='n')) cout<<"condition is true"; else cout<<"relational expression false"; return 0; } Output: Enter a number : 8 Enter any character : y condition is true output: Enter a number : 2 Enter any character : k relational expression false 
The ! (NOT) Operator:
The ! (NOT ) operator is also known as the unary operator. It inverts the value returned by the relational expression or the compound expression.
Example how to use logical operators ! (NOT) in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
#include <iostream> using namespace std; int main() { int a,b; cout<<"Enter 1st number : "; cin>>a; cout<<"Enter 2nt number : "; cin>>b; if(!(a<b)){ cout<<"1st is greater "; } else { cout<<"2nd is greater"; } return 0; } Output: Enter 1st number : 3 Enter 2nd number : 1 1st is greater Output: Enter 1st number : 5 Enter 2nd number : 6 2nd is greater 
Bitwise C/C++ Operators
The bitwise c++ operators are used to access the computer hardware. Use of bitwise operators requires knowledge of different number system and conversion numbers from one system into another number system. The data inside the computer is represented in binary form, i.e. in a sequence of 0s and 1s. 8bits are used to represent one character inside the computer. The bitwise c++ operators are used to manipulate the data inside the computer. These are also used in networking software to move data from one computer to another in the network. The commonly used bitwise operators are given below.
The Bitwise & (AND) c++ Operators:
The bitwise AND operator is used to compare the contents of rue operands (of int data type) on bitbybit basis. It returns 1 if he corresponding bit in both the operands is l otherwise it returns 0.
The Bitwise  (OR) c++ Operators:
The bitwise OR operator is used to compare the contents of two operands (of int data type) on bit by bit basis. It returns 1 of any one of the two bits is 1, otherwise it returns 0. It is represented by the vertical bar (  ).
The Bitwise ^ (XOR) C Operators:
The bitwise XOR operator is used to compare the contents to two operands (of int data type) one bitbybit basis. It returns 1 if the bits in the corresponding operands are different otherwise it returns 1 (if corresponding bits are same). It is represented by the caret ( ^ ) sign.
The Bitwise ~ (complement) C Operators:
The bitwise complement operator is used to operate on an expression. It invert the bits of the expression. It takes each bit of the operand and converts it to 1 if it is a 0 an to 0 it if is 1. It is represented by the tilde ( ~ ) sign.
The LeftShift (<<) c++ Operators
The left shift operator is used to shift a specified number of bits of an operand to the left. It has two operands. The first operand on the left handside of the operator is the constant or variable the operator is the constant or variable whose bits are to be shifted. The second operand on the righthandside of the operator specified the number of bits that are to be shifted to the left. The right shift operator is represented by the (<<) sign
The RightShift (>>) Operators :
The right shift operator is used to shift a specified number of bits of an operand to the right. It has two operands. The first operand at the left hand side of the operator is the constant or variable whose bits are to be shifted. The second operand at the right hand side specified the number of bits that are to be shifted to the right.when the specified number of the bits of the operand are shifted to the right then the same number of bits on the left are filled with 0s. The right shift operator is represented by the (>>) sign.
to understand what is Bitwise c++ operators in detail click on below link
Bitwise C operators AND OR XOR left shift right shift
The ‘sizeof’ c Operators:
In c / c++ data are mainly divided into three different types. These are:
Character data type:
Characters (letters digits and punctuation character) are represented by the char data type the short data type is the same size as char usually on byte.
Integer data type:
Integer data (whole numbers) are represented by int and long.
Real data type:
Numbers including decimal fractions are represented by float double or long double.
to find the size of these data type in c++ we have an operator which called sizeof operator.
The c operator sizeof is used to display the size in bytes of the data or data type. It has the following general format:
sizeof data
Or
sizeof (data type)
Example how to use sizeof operator in c/c++ programming:
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() { cout<<"size of integer is: "<<sizeof(int)<<endl; cout<<"size of float is: "<<sizeof(float)<<endl; cout<<"size of double is: "<<sizeof(double)<<endl; cout<<"size of character is: "<<sizeof(char); return 0; } Output: size of integer is: 4 size of float is: 4 size of double is: 8 size of character is: 1 
Comma c++ operators:
In the below program example comma operator is used comma operator is also known as sequence point operator. It is used to put more than one expression. on a single line separated by commas as used in the below program.
Example how to use comma operator in c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 
#include <iostream> using namespace std; int main() { int z= 5, result; result= z + 5,cout<<result; return 0; } Output: 10 
Priority of c++ operators:
If an expression has two or more operators then there is a certain order in which these operations are performed by the computer. This order is referred to as the order of precedence. The precedence order is given below
 All parentheses are evaluated first.
 Multiplication division and modulus operators have performed first these operators have the same level of precedence they are performed in left to right as they are encountered in an arithmetic expression. further, if an expression contains several like operations evaluation also proceeds from left to right.
 Addition and subtraction will be performed next. They also have same level of precedence so performance is given to the operator which occurs first.
Parentheses are used to change the priority order because c/c++ evaluates the expression in parentheses first if parentheses are nested then evaluation begins with the innermost pairs and works outward.