CPP Language / Operators

It is a symbol that tells the compiler to perform a particular operation. Operators in C++: Arithmetic Operators, Relational Operators, Logical Operators, Bitwise Operators, Assignment Operators and Misc Operators

Arithmetic Operators
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
++ Increment operator, increases integer value by one A++ will give 11
-- Decrement operator, decreases integer value by one A-- will give 9

Relational Operators
Operator Checks if the values of Example
== is equal to (A == B) is not true.
!= not equal to (A != B) is true.
>  greater than (A > B) is not true.
< less than (A < B) is true.
>gt;= greater than or equal to (A >= B) is not true.
<= less than or equal to (A <= B) is true.

Logical Operators
Operator Description Example
&& Logical AND operator (A && B) is false.
|| Logical OR Operator (A || B) is true.
! Logical NOT Operator !(A && B) is true.

Bitwise Operators
Operator Description Example
& Binary AND Operator (A & B)
| Binary OR Operator (A | B)
^ Binary XOR Operator (A ^ B)
~ Binary Ones Complement Operator (~A )
<<  Binary Left Shift Operator. A << 2
>> Binary Right Shift Operator. A >> 2

Assignment Operators
Operator Description
= Simple assignment operator
+= Add AND assignment operator
-= Subtract AND assignment operator
*= Multiply AND assignment operator.
/= Divide AND assignment operator.
%= Modulus AND assignment operator.
<<= Left shift AND assignment operator.
>>= Right shift AND assignment operator.
&= Bitwise AND assignment operator.
^= Bitwise exclusive OR and assignment operator.
|= Bitwise inclusive OR and assignment operator.

Misc Operators
S.No Operator Description Example
1 Sizeof() Variable size sizeof(VariableName)
2 Condition? X : Y If Condition is true it returns X value otherwise Y value. z=2>3?x:y; z=3
3 Comma operator (,) Used for comma separated list. --
4 dot(.) and arrow (-> ) Operators Used for reference individual members of classes, structures, and unions. --
5 Cast Used to convert one data type to other. int(4.3000) would return 4.
6 Pointer operator Returns address of a variable. Int a = 10,*ptr;
7 Pointer operator * Returns pointer variable value. Int a = 10,*ptr; printf(“%d”,*ptr);



Home     Back