Monday, 15 December 2025

C++ Operators

Operators are special symbols that perform operations on variables and values.

Example
int a = 5 + 3; // '+' is an addition operator
 
1. Arithmetic Operators
Used for mathematical calculations.
 
Operator        Description   Example
+                      Addition        a + b
-                       Subtraction   a - b
*                      Multiplication          a * b
/                      Division         a / b
%                     Modulus (remainder)         a % b
++                   Increment     ++a or a++
--                     Decrement    --a or a--
 
2. Relational Operators
Used to compare values (result is true or false).
 
Operator        Meaning                    Example
==                   Equal to                     a == b
!=                    Not equal to              a != b
>                     Greater than              a > b
<                     Less than                    a < b
>=                  Greater than or equal to     a >= b
<=                  Less than or equal to           a <= b

3. Logical Operators
Used to combine conditions.

Operator Meaning
&&                 Logical AND
`
!                 Logical NOT

4. Assignment Operators
Used to assign values.

Operator Example Meaning
=                 a = 5 Assign
+=                 a += 2 a = a + 2
-=                 a -= 2 a = a - 2
*=                 a *= 2 a = a * 2
/=                 a /= 2 a = a / 2
%=                 a %= 2 a = a % 2

5. Increment and Decrement Operators
Increase or decrease value by 1.

Operator Meaning
++                 Increment
--                 Decrement

Example:
int a = 5;
a++;   // a becomes 6

6. Bitwise Operators
Operate on bits.

Operator Meaning
&                 Bitwise AND
^                 Bitwise XOR
~                 Bitwise NOT
<<                 Left shift
>>                 Right shift

7. Conditional (Ternary) Operator
Short form of if-else.

condition ? value_if_true : value_if_false;

Example:
int max = (a > b) ? a : b;

8. Special Operators
Operator Use

sizeof         Size of variable
,                 Comma operator
.                 Member access
->                 Pointer member access
::                 Scope resolution

0 comments

Post a Comment