EZ

Eduzan

Learning Hub

Eduzan
Eduzan / C++

C++ Operators

Computer Science / C++ tutorial chapter - Published 2025-12-13 - C++

An operator is a symbol that acts on a value to perform specific mathematical or logical operations, forming the core of any programming language. In C++, a range of built-in operators is available to perform essential tasks and calculations.

Example:

c = a + b;

In this example, the + symbol is the addition operator, while a and b are the operands. The addition operator tells the compiler to sum the values of a and b.

Types of Operators in C

The C language provides a wide array of operators, categorized into six types:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Miscellaneous Operators

1. Arithmetic Operators in C : Arithmetic operators allow us to perform standard mathematical operations. In C, there are 9 arithmetic operators:

C++ Operators
SymbolOperatorDescriptionSyntax
+AdditionAdds two numeric values.a + b
-SubtractionSubtracts the right operand from the left.a - b
*MultiplicationMultiplies two numeric values.a * b
/DivisionDivides two numeric values.a / b
%ModulusGives the remainder of a division.a % b
+Unary PlusSpecifies positive values.+a
-Unary MinusReverses the sign of a value.-a
++IncrementIncreases the value of a variable by 1.a++
--DecrementDecreases the value of a variable by 1.a--

Example:

#include <stdio.h>

int main() {
    int a = 25, b = 5;

    printf("a + b = %d\n", a + b);
    printf("a - b = %d\n", a - b);
    printf("a * b = %d\n", a * b);
    printf("a / b = %d\n", a / b);
    printf("a %% b = %d\n", a % b);
    printf("+a = %d\n", +a);
    printf("-a = %d\n", -a);
    printf("a++ = %d\n", a++);
    printf("a-- = %d\n", a--);

    return 0;
}

Output:

a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0
+a = 25
-a = -25
a++ = 25
a-- = 26

2. Relational Operators in C : Relational operators are used to compare two operands. These operators return a boolean value (true or false), depending on the comparison result.

C++ Operators
SymbolOperatorDescriptionSyntax
<Less thanReturns true if the left operand is less than the right.a < b
>Greater thanReturns true if the left operand is greater than the right.a > b
<=Less than or equal toReturns true if the left operand is less than or equal to the right.a <= b
>=Greater than or equal toReturns true if the left operand is greater than or equal to the right.a >= b
==Equal toReturns true if both operands are equal.a == b
!=Not equal toReturns true if the operands are not equal.a != b

Example:

#include <stdio.h>

int main() {
    int a = 25, b = 5;

    printf("a < b  : %d\n", a < b);
    printf("a > b  : %d\n", a > b);
    printf("a <= b: %d\n", a <= b);
    printf("a >= b: %d\n", a >= b);
    printf("a == b: %d\n", a == b);
    printf("a != b : %d\n", a != b);

    return 0;
}

Output:

a < b  : 0
a > b  : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1

3. Logical Operators in C : Logical operators are used to combine multiple conditions. They evaluate to a boolean value (true or false).

C++ Operators
SymbolOperatorDescriptionSyntax
&&Logical ANDReturns true if both operands are true.a && b
` `Logical OR
!Logical NOTReturns true if the operand is false.!a

Example:

#include <stdio.h>

int main() {
    int a = 25, b = 5;

    printf("a && b : %d\n", a && b);
    printf("a || b : %d\n", a || b);
    printf("!a: %d\n", !a);

    return 0;
}

Output:

a && b : 1
a || b : 1
!a: 0
End of lesson.