Operators in C and C++
From Wikipedia, the free encyclopedia
This is a list of operators in the C++ and C programming languages. All the operators listed exist in C++; the third column indicates whether an operator is also present in C. It should also be noted that C does not support operator overloading.
The following operators are sequence points in both languages (when not overloaded): &&, ||, ?:, and , (the comma operator).
C++ also contains the type conversion operators const_cast, static_cast, dynamic_cast, and reinterpret_cast which are not listed in the table for brevity. The formatting of these operators means that their precedence level is unimportant.
Those operators that are in C, with the exception of the comma operator, are also in Java, Perl, C#, and PHP with the same precedence, associativity, and semantics.
Contents |
[edit] Operator precedence
The following is a table that lists the precedence and associativity of all the operators in the C++ programming language. Operators on the same row are evaluated with the same precedence, in the given direction.
Operators | Description | Associativity |
:: |
Scope resolution | N/A |
++ --
|
Postfix increment and decrement Function call |
left-to-right |
++ --
|
Prefix increment and decrement Unary plus and minus |
right-to-left |
.* ->* |
Pointer to member | left-to-right |
* / % |
Multiplication, division, and modulus (remainder) | |
+ - |
Addition and subtraction | |
<< >> |
Bitwise left shift and right shift | |
< <=
|
Relational “less than” and “less than or equal to” Relational “greater than” and “greater than or equal to” |
|
== != |
Relational “equal to” and “not equal to” | |
& |
Bitwise AND | |
^ |
Bitwise XOR (exclusive or) | |
| |
Bitwise OR (inclusive or) | |
&& |
Logical AND | |
|| |
Logical OR | |
c?t:f |
Ternary conditional | right-to-left |
=
|
Direct assignment Assignment by sum and difference |
|
, |
Comma | left-to-right |
[edit] Table
[edit] Arithmetic Operators |
|||
Operator Name | Syntax | Is overloadable? | In C? |
---|---|---|---|
Plus Operator (unary) | +a | Yes. | Yes. |
Addition Operator | a + b | Yes. | Yes. |
Prefix Increment Operator | ++a | Yes. | Yes. |
Postfix Increment Operator | a++ | Yes. | Yes. |
Addition Assignment Operator | a += b | Yes. | Yes. |
Negation Operator (unary) | -a | Yes. | Yes. |
Subtraction Operator | a - b | Yes. | Yes. |
Prefix Decrement Operator | --a | Yes. | Yes. |
Postfix Decrement Operator | a-- | Yes. | Yes. |
Subtraction Assignment Operator | a -= b | Yes. | Yes. |
Multiplication Operator | a * b | Yes. | Yes. |
Multiplication Assignment Operator | a *= b | Yes. | Yes. |
Division Operator | a / b | Yes. | Yes. |
Division Assignment Operator | a /= b | Yes. | Yes. |
Modulo Operator | a % b | Yes. | Yes. |
Modulo Assignment Operator | a %= b | Yes. | Yes. |
[edit] Comparison Operators |
|||
Operator Name | Syntax | Is overloadable? | In C? |
Less Than Operator | a < b | Yes. | Yes. |
Less Than Or Equal To Operator | a <= b | Yes. | Yes. |
Greater Than Operator | a > b | Yes. | Yes. |
Greater Than Or Equal To Operator | a >= b | Yes. | Yes. |
Not Equal To Operator | a != b | Yes. | Yes. |
Equal To Operator | a == b | Yes. | Yes. |
Logical Negation Operator | !a | Yes. | Yes. |
Logical And | a && b | Yes. | Yes. |
Logical Or | a || b | Yes. | Yes. |
[edit] Bitshift Operators |
|||
Operator Name | Syntax | Is overloadable? | In C? |
Left Shift | a << b | Yes. | Yes. |
Left Shift + Assignment | a <<= b | Yes. | Yes. |
Right Shift | a >> b | Yes. | Yes. |
Right Shift + Assignment | a >>= b | Yes. | Yes. |
[edit] Bitwise Operators |
|||
Operator Name | Syntax | Is overloadable? | In C? |
Bitwise Complement | ~a | Yes. | Yes. |
Bitwise And | a & b | Yes. | Yes. |
Bitwise And + Assignment | a &= b | Yes. | Yes. |
Bitwise Or | a | b | Yes. | Yes. |
Bitwise Or + Assignment | a |= b | Yes. | Yes. |
Bitwise Xor | a ^ b | Yes. | Yes. |
Bitwise Xor + Assignment | a ^= b | Yes. | Yes. |
[edit] Other Operators |
|||
Operator Name | Syntax | Is overloadable? | In C? |
Assignment Operator | a = b | Yes. | Yes. |
Function Call Operator | a() | Yes. | Yes. |
Array Operator | a[b] | Yes. | Yes. |
Dereference Operator | *a | Yes. | Yes. |
Reference Operator | &a | Yes. | Yes. |
Arrow Operator | a->b | Yes. | Yes. |
Member Operator | a.b | No. | Yes. |
Dereferencing Member Operator | a.*b | No. | No. |
Dereferencing Arrow Operator | a->*b | Yes. | No. |
Cast Operator | (type) a | Yes. | Yes. |
Comma Operator | a , b | Yes. | Yes. |
Conditional | a ? b : c | No. | Yes. |
Scope Resolution | a :: b | No. | No. |
Sizeof Operator | sizeof a | No. | Yes. |
Type Identification Operator | typeid (type) | No. | No. |
new Operator | new type | Yes. | No. |
delete Operator | delete a | Yes. | No. |
[edit] Language Extensions |
|||
Operator Name | Syntax | Is overloadable? | Vendor |
Label Value Operator | && label | ??? | GCC |
[edit] Notes
Many of the operators containing multi-character sequences are given "names" built from the operator name of each character. For example, += and -= are often called plus equal and minus equal.
The binding of operators in C and C++ is specified (in the corresponding Standards) by a factored language grammar, rather than a precedence table. This creates some subtle conflicts. For example, in C, the syntax for a conditional expression is:
logical-OR-expression ? expression : conditional-expression
while in C++ it is:
logical-or-expression ? expression : assignment-expression
Hence, the expression:
e = a ? b : c = d
is parsed differently in the two languages. In C, this expression is parsed as:
e = ((a ? b : c) = d)
which is a semantic error, since the result of a conditional-expression is not an lvalue. In C++, it is parsed as:
e = (a ? b : (c = d))
which is a valid expression.
[edit] External links
- Experimental results showing that developers have poor knowledge of binary operator precedence.