Jump to content

Operators in C and C++: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎Table: Dividend -> Quotient (dividend is left operand, quotient is result) [self]
No edit summary
Line 1: Line 1:
{{Mergefrom|Comma operator|date=July 2007}}

This is a list of [[operator (programming)|operator]]s in the [[C++]] and [[C (programming language)|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]].
This is a list of [[operator (programming)|operator]]s in the [[C++]] and [[C (programming language)|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]].



Revision as of 19:18, 14 February 2008

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, with one exception: ternary operator associativity in PHP is left-to-right.

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 in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction.

The syntax of expressions in C and C++ is specified by a context-free grammar. The table given here has been inferred from the grammar.

A precedence table, while mostly adequate, cannot resolve a few details. In particular, note that the ternary operator allows any arbitrary expression as its middle operand, despite being listed as having higher precedence than the assignment and comma operators. Thus a ? b , c : d is interpreted as a ? (b, c) : d, and not as the meaningless (a ? b), (c : d). Also, note that the immediate, unparenthesized result of a C cast expression cannot be the operand of sizeof. Therefore, sizeof (int) * x is interpreted as (sizeof(int)) * x and not sizeof ((int) *x).

Operator(s) Description Associativity
:: Scope resolution (C++ only) Left-to-Right
++ --
()
[]
.
->
typeid()
const_cast dynamic_cast
reinterpret_cast static_cast
Postfix increment and decrement
Function call
Array subscripting
Element selection by reference
Element selection through pointer
Run-time type information (C++ only)
Type cast (C++ only)
Type cast (C++ only)
++ --
+ -
! ~
(type)
*
&
sizeof
new new[]
delete delete[]
Prefix increment and decrement
Unary plus and minus
Logical NOT and bitwise NOT
Type cast
Indirection (dereference)
Address-of (reference)
Size-of
Dynamic memory allocation (C++ only)
Dynamic memory deallocation (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 left shift and right shift
Assignment by bitwise AND, XOR, and OR
throw Throw operator (exceptions throwing, C++ only) Not available
, Comma Left-to-Right

Table

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

Arithmetic operators

Operator Name Syntax Overloadable Included 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 (Quotient) a / b Yes Yes
Assignment by Division a /= b Yes Yes
Modulus (Remainder) a % b Yes Yes
Assignment by Modulus a %= b Yes Yes

Comparison operators

Operator Name Syntax Overloadable Included 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

Bitwise operators

Operator Name Syntax Overloadable Included 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

Other operators

Operator Name Syntax Overloadable Included 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
sizeof(type)
No Yes
Type Identification typeid(a)
typeid(type)
No No
Allocate Storage new type Yes No
Allocate Storage (Array) new type[n] Yes No
Deallocate Storage delete a Yes No
Deallocate Storage (Array) delete[] a Yes No

Language extensions

Operator Name Syntax Overloadable Vendor
Label Value && label ? GCC

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 arithmetic 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.

C++ operator synonyms

C++ defines keywords to act as aliases for a number of symbols that function as operators: and (&&), bitand (&), and_eq (&=), or (||), bitor (|), or_eq (|=), xor (^), xor_eq (^=), not (!), not_eq (!=), compl (~). These are parsed exactly like their symbolic equivalents, and can be used in place of the symbol they replace. It is the punctuation that is aliased, not the operators. For example, bitand can replace both the bitwise AND operator and the address-of operator.

C provides the header file iso646.h, which defines these symbols as preprocessor macros which expand to the values these symbols take on in C++. For compatibility with C, C++ provides the header ciso646 for which inclusion has no effect.

External links