Namespaces
Variants
Views
Actions

Operators

From cppreference.com


Operators are a basic feature of the C++ language, which, similar to operators in mathematics, allow the production of a result of computation from one, or a combination of two variables. There are roughly 60 operators in C++; fortunately, you only need to know a few of them to get started writing programs.

Contents

[edit] Assignment operator

The assignment operator (=) assigns a value to a variable. For example:

b = 14;

This statement assigns the integer value 14 to the variable b. The assignment operator always works from right to left. For example:

c = b;

Here the variable c is assigned the value that is held in b. The value stored in b is left unmodified, whereas the previous value of c is lost.

Below is an example that shows how to use assignment operator to swap two values:

#include <iostream>
 
int main()
{
    int x = 10;
    int y = 20;
    std::cout << "x: " << x << "; y: " << y << "\n";
 
    int temp = x; // a temporary variable to hold the old value of x
    x = y;
    y = temp;
 
    std::cout << "x: " << x << "; y: " << y << "\n";
    return 0;
}

Output:

x: 10; y: 20
x: 20; y: 10

[edit] Arithmetic operators

The arithmetic operators compute a new result from two given values. The following arithmetic operators are available in C++:

  • addition. Example: a + b. Here the sum of a and b is calculated.
  • subtraction. Example: a - b. Here b is subtracted from a.
  • multiplication. Example: a * b. Here the multiplication of a and b is performed.
  • division. Example: a / b. Here a is divided by b. For integer types, non-integer results are rounded towards zero (truncated).
  • modulo. Example: a % b. Here the remainder of the division of a by b is calculated.

The below example demonstrates use of the arithmetic operators:

#include <iostream>
 
int main()
{
    int a = 14;
    int b = 5;
    int c = 12;
 
    std::cout << "a: " << a 
              << "; b: " << b 
              << "; c: " << c << "\n";
 
    std::cout << "a+b: " << (a + b)
              << "; b+c: " << (b + c) 
              << "; a+c: " << (a + c) << "\n";
 
    std::cout << "a-b: " << (a - b) 
              << "; b-c: " << (b - c) 
              << "; a-c: " << (a - c) << "\n";
 
    std::cout << "a*b: " << (a * b) 
              << "; b*c: " << (b * c) 
              << "; a*c: " << (a * c) << "\n";
 
    std::cout << "a/b: " << (a / b) 
              << "; b/c: " << (b / c) 
              << "; a/c: " << (a / c) << "\n";
 
    std::cout << "a%b: " << (a % b) 
              << "; b%c: " << (b % c) 
              << "; a%c: " << (a % c) << "\n";
 
    return 0;
}

Output:

a: 14; b: 5; c: 12
a+b: 19; b+c: 17; a+c: 26
a-b: 9; b-c: -7; a-c: 2
a*b: 70; b*c: 60; a*c: 168
a/b: 2; b/c: 0; a/c: 1
a%b: 4; b%c: 5; a%c: 2


[edit] Bitwise logical operators

[edit] Bitwise shift operators

[edit] Compound assignment operators

[edit] Increment and decrement operators

[edit] Logical operators

[edit] Comparison operators

Comparison operators allow to determine the relation of two different values. The following operators are available in C++:

  • less-than. Example: a < b: Yields true if the value on the left (a) side is less (smaller) than the value on the right side (b).
  • less-or-equal. Example: a <= b: Yields true if the value of a is less than or equal to the value of b.
  • equality. Example: a == b: Yields true if the value of a is equal to the value of b.
  • greater-or-equal. Example: a >= b: Yields true if the value of a is greater than or equal to the value of b.
  • greater-than. Example: a > b: Yields true if the value of a side is greater than the value of b.
  • non-equality. Example: a != b: Yields true if the value of a is not equal to the value of b.

The following example demonstrates use of the comparison operators:

#include <iostream>
 
int main()
{
    int a = 14;
    int b = 5;
 
    if (a < b) {
        std::cout << a << " is less than " << b << std::endl;
    }
    else if (a == b) {
        std::cout << a << " and " << b << " are equal" << std::endl;
    }
    else /* a > b */ {
        std::cout << a << " is greater than " << b << std::endl;
    }
 
    return 0;
}

Output:

14 is greater than 5

Be aware that comparison of floating point values may sometimes yield unexpected results due to rounding effects. Therefore, it is recommended to always use <= or >= comparison with floating point values, instead of checking for equality with ==.

[edit] Other operators

There are several other operators that we will learn about later.