C Plus Plus Operators
C Plus Plus Operators
C++ language
is rich with built-in operators.
Below code will explain you that pre-increment first increments the value then assigns the value to the variable. Post
increment first assigns the value then increments it.
Below code displays the value of varibles using pre and post increment.
int main()
{
int i = 0;
while(i < 10)
{
cout << i++; //post increment
}
cout << endl;
i = 0;
while(i<10)
{
cout << ++i; //pre increment
}
return 0;
}
Below is the output of above program
0123456789
12345678910
Relational Operators
This category of the operators is used to compare different values. The result of the operation is a Boolean value
(true or false). The relational operators are used in the following form Operand1 operatorOperand2, where
operands can be some variables or literals that are compared.
Operator Description
== Is equal to (Returns true if the operands are equal. Otherwise false.)
!= Is not equal to (Returns true if the operands are not equal. Otherwise
false.)
> Greater than (Returns true if the operand1 is greater than operand2.
Otherwise false.)
< Less than (Returns true if the operand1 is less than operand2. Otherwise
false.)
>= Greater than or equal to (Returns true if the operand1 is greater than
operand2 or equal to operand 2. Otherwise false.)
<= Less than or equal to (Returns true if the operand1 is less than operand2
or equal to operand 2. Otherwise false.)
Now we can try these operators. Remember, that "cout" outputs 1 for true and 0 for false:
int three = 3;
int five = 5;
cout << " 3 is equal to 5 = " << (three == five) << endl;
cout << " 3 is not equal to 5 = " << (three != five) << endl;
cout << " 3 is less than 5 = " << (three < five) << endl;
cout << " 3 is greater than 5 = " << (three > five) << endl;
cout << " 3 is not less than 5 = " << (three >= five) << endl;
cout << " 3 is not greater than 5 = " << (three <= five) << endl;
Logical Operators
Operator Description
&& And operator. Performs a logical conjunction on two expressions.
(if both expressions evaluate to True, result is True. If either expression evaluates to
False, result is False)
|| Or operator. Performs a logical disjunction on two expressions.
(if either or both expressions evaluate to True, result is True)
! Not operator. Performs logical negation on an expression.
Bitwise Operators
Bitwise operators are similar to the logic operators, but they perform the same logical operations on bits. All data in
memory is represented in the binary form. So, variables in form of bits contains only 0 or 1.
Operator Description
<< Binary Left Shift Operator
>> Binary Right Shift Operator
~ Binary Ones Complement
Operator
& Binary AND Operator
^ Binary XOR Operator
| Binary OR Operator
The resultant bit is set to 1 if and only if both variables have 1 in the corresponding bit. Example of binary &:
The resultant bit is set to 1 if at least one of the variables has 1 in the corresponding bit. Example of binary |:
The result bit is set to 1 if only one of the variables has 1 in the corresponding bit. Example of Binary xor:
~10100110 = 01011001
Operator Description
= Assign
+= Increments, then assigns
-= Decrements, then assigns
*= Multiplies, then assigns
/= Divides, then assigns
%= Modulus, then assigns
<<= Left shift and assigns
>>= Right shift and assigns
&= Bitwise AND assigns
^= Bitwise exclusive OR and assigns
|= Bitwise inclusive OR and assigns
//you can add value in a more compact way by combining two operators
k += 7; // this is equal to k = k + 7
Composite
Assignment Example Is Equivalent to Output
Operator
+= X += 2 X=X+2 12
-= X -= 2 X=X-2 8
*= X *= 2 X=X*2 20
/= X /= 2 X=X/2 5
%= X %= 2 X=X%2 0
<<= X <<= 2 X = X << 2 40
>>= X >>= 2 X = X >> 2 2
&= X &= 2 X=X&2 2
^= X ^= 2 X=X^2 8
|= X |= 2 X=X|2 10
It is suggested to use Composite Assignment Operator for more efficient code
X +=10; // will be faster than X = X + 10
X = X + 10; // will be slower than X += 10
Misc Operators
Operator Description
, Comma operator
sizeof() Returns the size of an memory location.
& Returns the address of an memory location.
* Pointer to a variable.
?: Conditional Expression
There are some operators that are not included in any categories listed above, but they are useful and can be used
in your programs:
"sizeof" Operator
sizeof operator returns the size of the variable or type. Here "size" means how many bytes of memory is being used
by a variable or type.
Coma "," Operator is used when you need to perform a sequence of operations. You used it when initialized a list of
variable of the same type:
double a = 2.0,
b = 4.3,
c = 2;
Operator Precedence
Operator precedence determines how an expression is evaluated. Some operators will have higher precedence
than others. For example, multiplication operator will have higher precedence than addition operator.
For example a = 2 + 3 * 5; here, a will be assigned 17, not 25 because operator * has higher precedence than +, so
3*5 gets multiplied first and then 2 gets added to the multiplication result of 3*5.
In the table below, operators with the highest precedence appear at the top of the table and operators with the
lowest precedence appear at the bottom. In the expression, higher precedence operators will be evaluated first.