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 and the arrow 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 are listed top to bottom in descending precedence and operators that are on the same row are evaluated with the same precedence, in the given direction.

Operator(s) Description Associativity
:: Scope resolution (C++ only) N/A
++ --
()
[]
.
->
Postfix increment and decrement
Function call
Array subscript
Element selection by reference
Element selection by pointer
Left-to-Right
++ --
+ -
! ~
(type)
*
&
sizeof
new delete
Prefix increment and decrement
Unary plus and minus
Logical NOT and bitwise one's complement
Type cast
Indirection (dereference)
Address-of (reference)
Size-of
Dynamic memory (de-)allocation (C++ only)
Right-to-Left
.* ->* Pointer to member (C++ only) 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 (see ?:) Right-to-Left
=
+= -=
*= /= %=
<<= >>=
&= ^= |=
Direct assignment
Assignment by sum and difference
Assignment by product, dividend, and remainder
Assignment by bitwise shift
Assignment by bitwise AND, XOR, and OR
, Comma Left-to-Right

[edit] Table

For the purposes of this table, a, b, and c represent valid values (literals, values from variables, or return values), object names, or lvalues, as appropriate.

[edit] Arithmetic Operators

Operator Name Syntax Overloadable In C
Unary Plus +a Yes Yes
Addition (Sum) a + b Yes Yes
Prefix Increment ++a Yes Yes
Postfix Increment a++ Yes Yes
Assignment by Addition a += b Yes Yes
Unary Minus (Negation) -a Yes Yes
Subtraction (Difference) a - b Yes Yes
Prefix Decrement --a Yes Yes
Postfix Decrement a-- Yes Yes
Assignment by Subtraction a -= b Yes Yes
Multiplication (Product) a * b Yes Yes
Assignment by Multiplication a *= b Yes Yes
Division (Dividend) a / b Yes Yes
Assignment by Division a /= b Yes Yes
Modulus (Remainder) a % b Yes Yes
Assignment by Modulus a %= b Yes Yes

[edit] Comparison Operators

Operator Name Syntax Overloadable In C
Less Than a < b Yes Yes
Less Than or Equal To a <= b Yes Yes
Greater Than a > b Yes Yes
Greater Than or Equal To a >= b Yes Yes
Not Equal To a != b Yes Yes
Equal To a == b Yes Yes
Logical Negation !a Yes Yes
Logical AND a && b Yes Yes
Logical OR a || b Yes Yes

[edit] Bitwise Operators

Operator Name Syntax Overloadable In C
Bitwise Left Shift a << b Yes Yes
Assignment by Bitwise Left Shift a <<= b Yes Yes
Bitwise Right Shift a >> b Yes Yes
Assignment by Bitwise Right Shift a >>= b Yes Yes
Bitwise One's Complement ~a Yes Yes
Bitwise AND a & b Yes Yes
Assignment by Bitwise AND a &= b Yes Yes
Bitwise OR a | b Yes Yes
Assignment by Bitwise OR a |= b Yes Yes
Bitwise XOR a ^ b Yes Yes
Assignment by Bitwise XOR a ^= b Yes Yes

[edit] Other Operators

Operator Name Syntax Overloadable In C
Basic Assignment a = b Yes Yes
Function Call a() Yes Yes
Array Subscript a[b] Yes Yes
Indirection (Dereference) *a Yes Yes
Address-of (Reference) &a Yes Yes
Member by Pointer a->b Yes Yes
Member a.b No Yes
Member by Pointer Indirection a->*b Yes No
Member Indirection a.*b No No
Cast (type) a Yes Yes
Comma a , b Yes Yes
Ternary Conditional a ? b : c No Yes
Scope Resolution a::b No No
Size-of sizeof a No Yes
Type Identification typeid type No No
Allocate Storage new type Yes No
Deallocate Storage delete a Yes No

[edit] Language Extensions

Operator Name Syntax Overloadable Vendor
Label Value && 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(s) and minus equal(s), instead of the more verbose "assignment by addition" and "assignment by subtraction".

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.

The precedence of the bitwise logical operators has been criticized [[1]]. Conceptually, & and | are numerical operators like + and *. But the expression

   a & b == 7

means

   a & (b == 7),

while

   a + b == 7

means

   (a + b) == 7.

This requires parentheses to be used more often than they otherwise would.

[edit] External links

C programming language
Libraries: C standard library | glibc | Dietlibc | uClibc | Newlib
History: Criticism of the C programming language
Language Features: String | Syntax | Preprocessor | Variable types and declarations | Functions
Dialects: C++ | Objective-C
C and Other Languages: Compatibility of C and C++ | Operators in C and C++ | Comparison of Pascal and C | C to Java byte-code compiler
In other languages