
This is a list of operators in the C and C++ programming languages.
All listed operators are in C++ and lacking indication otherwise, in C as well. Some tables include a "In C" column that indicates whether an operator is also in C. Note that C does not support operator overloading.
When not overloaded, for the operators &&, ||, and , (the comma operator), there is a sequence point after the evaluation of the first operand.
Most of the operators available in C and C++ are also available in other C-family languages such as C#, D, Java, Perl, and PHP with the same precedence, associativity, and semantics.
Many operators specified by a sequence of symbols are commonly referred to by a name that consists of the name of each symbol. For example, += and -= are often called "plus equal(s)" and "minus equal(s)", instead of the more verbose "assignment by addition" and "assignment by subtraction".
In the following tables, lower case letters such as a and b represent literal values, object/variable names, or l-values, as appropriate. R, S and T stand for a data type, and K for a class or enumeration type. Some operators have alternative spellings using digraphs and trigraphs or operator synonyms.
C and C++ have the same arithmetic operators and all can be overloaded in C++.
| Operation | Syntax | C++ prototype | |||
|---|---|---|---|---|---|
| in class K | outside class | ||||
| colspan="2" | |||||
| colspan="2" | Subtraction | a '''-''' b | |||
| colspan="2" | Unary plus; integer promotion | '''+'''a | |||
| colspan="2" | '''-'''a | ||||
| colspan="2" | Multiplication | a '''*''' b | |||
| colspan="2" | Division | a '''/''' b | |||
| colspan="2" | Modulo | a '''%''' b | |||
| colspan="2" | Prefix increment | '''++'''a | |||
| colspan="2" | Postfix increment | a'''++''' | |||
| colspan="2" | Prefix decrement | '''--'''a | |||
| colspan="2" | Postfix decrement | a'''--''' | |||
All relational (comparison) operators can be overloaded in C++. Since C++20, the inequality operator is automatically generated if operator== is defined and all four relational operators are automatically generated if operator<=> is defined.[1]
| Operation | Syntax | In C | C++ prototype | |||
|---|---|---|---|---|---|---|
| in class K | outside class | |||||
| colspan="2" | Equal to | a '''==''' b | ||||
| colspan="2" | Not equal to | a ''' | = b | |||
| colspan="2" | Greater than | a '''>''' b | ||||
| colspan="2" | Less than | a '''<''' b | ||||
| colspan="2" | Greater than or equal to | a '''>=''' b | ||||
| colspan="2" | Less than or equal to | a '''<=''' b | ||||
| colspan="2" | Three-way comparison | a '''<=>''' b | ||||
C and C++ have the same logical operators and all can be overloaded in C++.
Note that overloading logical AND and OR is discouraged, because as overloaded operators they always evaluate both operands instead of providing the normal semantics of short-circuit evaluation.[2]
| Operation | Syntax | C++ prototype | |||
|---|---|---|---|---|---|
| in class K | outside class | ||||
| colspan="2" | ''' | a | |||
| colspan="2" | a '''&&''' b | ||||
| colspan="2" | a '''<nowiki>||</nowiki>''' b | ||||
C and C++ have the same bitwise operators and all can be overloaded in C++.
| Operation | Syntax | C++ prototype | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| in class K | outside class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| colspan="2" | NOT | '''~'''a | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| colspan="2" | AND | a '''&''' b | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| colspan="2" | OR | a '''<nowiki>|</nowiki>''' b| | |-| colspan="2" | XOR| style="text-align:center;" | a '''^''' b| | |- | colspan="2" | Shift left| style="text-align:center;" | a '''<<''' b | | |-| colspan="2" | Shift right| style="text-align:center;" | a '''>>''' b | | |}AssignmentC and C++ have the same assignment operators and all can be overloaded in C++. For the combination operators,
SynonymsC++ defines keywords to act as aliases for a number of operators:[6]
Each keyword is a different way to specify an operator and as such can be used instead of the corresponding symbolic variation. For example, and specify the same behavior. As another example, the The ISO C specification makes allowance for these keywords as preprocessor macros in the header file . For compatibility with C, C++ also provides the header, the inclusion of which has no effect. Until C++20, it also provided the corresponding header which had no effect as well. Expression evaluation orderDuring expression evaluation, the order in which sub-expressions are evaluated is determined by precedence and associativity. An operator with higher precedence is evaluated before a operator of lower precedence and the operands of an operator are evaluated based on associativity. The following table describes the precedence and associativity of the C and C++ operators. Operators are shown in groups of equal precedence with groups ordered in descending precedence from top to bottom (lower order is higher precedence).[7] [8] [9] Operator precedence is not affected by overloading.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||