# What are the most important C++ operators?

C++ operators are essential for programming with C++. Whether you are just beginning to [learn C++](https://www.ionos.ca/digitalguide/websites/web-development/learn-c/) or have been working with the programming language for a while, operators can make programming simpler and more efficient. The function of some operators can be deduced from their name, while others may take some time to memorize.

## What are C++ operators?

An operator is a **sign for an operation**, which is applied to at least one operand, but in most cases to several. This results in a new value in most cases. A popular example of these are the arithmetic operators you learn in school, for example, “+” for addition and “-” for subtraction.

C++ operators are not only distinguishable according to their functional purpose. The operator’s **arity** is also an important criterion:

<table>
  <thead>
    <tr>
      <th><strong>Arity</strong></th>
      <th><strong>Number of operands</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Unary operators</td>
      <td>One operand</td>
      <td>Logical negation: !var_name</td>
    </tr>
    <tr>
      <td>Binary operators</td>
      <td>Two operand</td>
      <td>Addition: value1 + value2</td>
    </tr>
    <tr>
      <td>Ternary operators</td>
      <td>Three operands</td>
      <td>If-else-condition: condition? condition_true : condition_false</td>
    </tr>
  </tbody>
</table>

## What is operator precedence?

As with arithmetic operators in school or [Python operators](https://www.ionos.ca/digitalguide/websites/web-development/python-operators/ "Python operators"), there is an operator precedence for C++ operators. This specifies the **order in which the operators should be evaluated**. The dot before dash rule applies for arithmetic operators, however, there are other rules for other C++ operators.

```C++
if (var1 && var2 || var3) {
    do_something();
}
```

The example above shows the logical expression being evaluated after the if operator. The &amp;&amp; operator (logical And) has priority over the \|\| operator (logical Or). So, if the evaluation of “var1 &amp;&amp; var2” or the evaluation of “var3” returns “true”, the “do\_something()” function call is executed.

You can also use **brackets** to be on the safe side.

### How to overload C++ operators

You can overload most C++ operators. This means that you can assign **a new meaning** to an existing operator **in a context**. To perform an operator overload in C++, you need the **keyword “operator”**. When overloading, this keyword should be put before the C++ operator. Operator overloading in C++ will behave like function overloading.

Tip [Webspace hosting](https://www.ionos.ca/hosting/webspace "Webspace from IONOS") from IONOS is perfect for backing up your C++ projects online. You can install it with just one click.

## An overview of C++ operators

### Arithmetic operators

You probably already know the arithmetic C++ operators from school. They operate on numbers and return a new number. The arithmetic operators are all binary operators, except for the unary plus and unary minus.

<table>
  <thead>
    <tr>
      <th><strong>C++ operator</strong></th>
      <th><strong>Meaning</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>+</td>
      <td>Addition / unary plus</td>
      <td>6 + 4</td>
    </tr>
    <tr>
      <td>-</td>
      <td>Subtraction / unary minus</td>
      <td>10 - 6</td>
    </tr>
    <tr>
      <td>*</td>
      <td>Multiplication</td>
      <td>10*  3</td>
    </tr>
    <tr>
      <td>/</td>
      <td>Integer division</td>
      <td>20 / 10</td>
    </tr>
    <tr>
      <td>%</td>
      <td>Modulo</td>
      <td>21 % 2</td>
    </tr>
  </tbody>
</table>

### Assignment operators

Similar to other programming languages, values are stored in variables. You need special operators to assign concrete values to these variables.

#### Simple assignment operators

<table>
  <thead>
    <tr>
      <th><strong>C++ operator</strong></th>
      <th><strong>Meaning</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>=</td>
      <td>Simple assignment</td>
      <td>x = 3</td>
    </tr>
    <tr>
      <td>++</td>
      <td>Increment</td>
      <td>x++</td>
    </tr>
    <tr>
      <td>--</td>
      <td>Decrement</td>
      <td>x--</td>
    </tr>
  </tbody>
</table>

#### Combined assignment operators

In addition to the simple assignment operators, C++ also supports combined operators. These are arithmetic or bitwise operations which are **simultaneously combined with a value assignment**:

```C++
int x = 4;
x += 2;
```

The code example above shows that the variable x assigned the numeric value 4 with a simple assignment. The combined assignment operator “+=” is used to perform an arithmetic addition after this and saves the resulting value directly in x. The assignment would be “x = x + 2” after being written out.

<table>
  <thead>
    <tr>
      <th><strong>C++ operator</strong></th>
      <th><strong>Meaning</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>+=</td>
      <td>Addition and assignment</td>
      <td>x += 2</td>
    </tr>
    <tr>
      <td>-=</td>
      <td>Subtraction and assignment</td>
      <td>x -= 2</td>
    </tr>
    <tr>
      <td>*=</td>
      <td>Multiplication and assignment</td>
      <td>x* = 2</td>
    </tr>
    <tr>
      <td>/=</td>
      <td>Division and assignment</td>
      <td>x /= 2</td>
    </tr>
    <tr>
      <td>%=</td>
      <td>Modulo and assignment</td>
      <td>x %= 2</td>
    </tr>
    <tr>
      <td>&amp;=</td>
      <td>Bitwise And and assignment</td>
      <td>b &amp;= 1</td>
    </tr>
    <tr>
      <td>&lt;&lt;=</td>
      <td>Bitshift left and assignment</td>
      <td>b &lt;&lt;= 1</td>
    </tr>
    <tr>
      <td>&gt;&gt;=</td>
      <td>Bitshift right and assignment</td>
      <td>b &gt;&gt;= 1</td>
    </tr>
    <tr>
      <td>^=</td>
      <td>Bitwise Xor and assignment</td>
      <td>b ^= 1</td>
    </tr>
    <tr>
      <td>`</td>
      <td>=`</td>
      <td>Bitwise Or and assignment</td>
    </tr>
  </tbody>
</table>

### Logical C++ operators

You can use the C++ logical operators for propositional comparisons of two expressions. Logical operators are binary, except for the Logical Not, which only refers to one statement and negates its truth value.

<table>
  <thead>
    <tr>
      <th><strong>C++ operator</strong></th>
      <th><strong>Meaning</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>&amp;&amp;</td>
      <td>Logical And</td>
      <td>true &amp;&amp; true</td>
    </tr>
    <tr>
      <td>`</td>
      <td></td>
      <td>`</td>
    </tr>
    <tr>
      <td>!</td>
      <td>Logical Not</td>
      <td>!true</td>
    </tr>
  </tbody>
</table>

### Comparison operators

Comparison operators are C++ operators that examine the relationship between two elements. They are binary, except for the three-way comparison, which returns a number. The return value of all C++ comparison operators is a truth value.

<table>
  <thead>
    <tr>
      <th><strong>C++ operator</strong></th>
      <th><strong>Meaning</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>==</td>
      <td>Equality</td>
      <td>a == b</td>
    </tr>
    <tr>
      <td>!=</td>
      <td>Inequality</td>
      <td>a != b</td>
    </tr>
    <tr>
      <td>&lt;=</td>
      <td>Smaller or equal</td>
      <td>a &lt;= b</td>
    </tr>
    <tr>
      <td>&gt;=</td>
      <td>Greater or equal</td>
      <td>a &gt;= b</td>
    </tr>
    <tr>
      <td>&lt;</td>
      <td>Smaller</td>
      <td>a &lt; b</td>
    </tr>
    <tr>
      <td>&gt;</td>
      <td>Larger</td>
      <td>a &gt; b</td>
    </tr>
    <tr>
      <td>&lt;=&gt;</td>
      <td>Three-way comparison</td>
      <td>a &lt;=&gt; b</td>
    </tr>
  </tbody>
</table>

### Bit manipulation

Bitwise C++ operators access individual bits efficiently and improve the speed of programs. They are especially important for performance-oriented programming.

<table>
  <thead>
    <tr>
      <th><strong>C++ operator</strong></th>
      <th><strong>Meaning</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>&amp;</td>
      <td>Bitwise And</td>
      <td>a &amp; b</td>
    </tr>
    <tr>
      <td>`</td>
      <td>`</td>
      <td>Bitwise Or</td>
    </tr>
    <tr>
      <td>^</td>
      <td>Bitwise Xor</td>
      <td>a ^ b</td>
    </tr>
    <tr>
      <td>~</td>
      <td>Bitwise negation</td>
      <td>~a</td>
    </tr>
    <tr>
      <td>&lt;&lt;</td>
      <td>Left shift</td>
      <td>a &lt;&lt; b</td>
    </tr>
    <tr>
      <td>&gt;&gt;</td>
      <td>Right shift</td>
      <td>a &gt;&gt; b</td>
    </tr>
  </tbody>
</table>

### Memory management

C++ is a machine-oriented language and has a several operators for memory management.

<table>
  <thead>
    <tr>
      <th><strong>C++ operator</strong></th>
      <th><strong>Meaning</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>&amp;</td>
      <td>Address determination</td>
      <td>&amp;x</td>
    </tr>
    <tr>
      <td>sizeof()</td>
      <td>Determines the memory requirement of an expression</td>
      <td>sizeof(x)</td>
    </tr>
    <tr>
      <td>new</td>
      <td>Creates a new object and returns pointer</td>
      <td>object* pointer = new object()</td>
    </tr>
    <tr>
      <td>delete</td>
      <td>Destroys an object</td>
      <td>delete object</td>
    </tr>
  </tbody>
</table>

### Data access for objects and pointers

The following C++ operators will help you access individual members of objects or the memory areas of pointers.

<table>
  <thead>
    <tr>
      <th><strong>C++ operator</strong></th>
      <th><strong>Meaning</strong></th>
      <th><strong>Example</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>*</td>
      <td>Dereferencing pointers, returns memory area</td>
      <td>\* pointer = 3;</td>
    </tr>
    <tr>
      <td>.</td>
      <td>Accesses members of an object</td>
      <td>object.member = 2;</td>
    </tr>
    <tr>
      <td>-&gt;</td>
      <td>Accesses members of an object with a pointer</td>
      <td>objectpointer-&gt;member = 2;</td>
    </tr>
  </tbody>
</table>


This is a markdown version of: [https://www.ionos.ca/digitalguide/websites/web-development/c-operators/](https://www.ionos.ca/digitalguide/websites/web-development/c-operators/) for AI/LLM consumption.