Skip to content

operators

bergsma edited this page Jul 31, 2014 · 2 revisions

Operators

Operands can be variables, literals, or results from expressions. Operands are used with primary operators, binary operators, unary operators, and the comma operator to construct expressions.

Operator Summary

The following table groups operators with the same precedence separated by empty rows. Operators in higher groups have higher precedence than operators in lower groups. Operator Category Description Usage

. unary member selection variable . member

[ ] primary subscripting variable [ expr ]

( ) primary function call function ( expr_list )

( ) primary parenthesis ( expr )

++ unary post increment expr ++

-- unary post decrement expr --

++ unary pre increment ++ expr

-- unary post increment -- expr

! unary not ! expr

unary minus

  • expr

unary plus

  • expr

& unary referenceof & variable

* unary dereference * reference

-> unary member dereference reference -> member

* binary multiply expr * expr

/ binary divide expr / expr

% binary modulus expr % expr

binary addor concatenate expr + expr

binary subtractor dissociate expr - expr

< binary less than expr < expr

<= binary less than or equal expr <= expr

> binary greater than expr > expr

>= binary greater than or equal expr >= expr

== binary equal to expr == expr

!= binary not equal to expr != expr

& binary bitwise and expr & expr

^ binary bitwise xor expr ^ expr

| binary bitwise or expr | expr

&& binary logical and expr && expr

|| binary logical or expr || expr

?: binary conditional expr ? expr : expr

= binary assignment variable = expr

, comma list expr , expr

String Operators

The operators, + and -, have special meaning for strings. The + operator performs string concatenation, while the - operator performs string dissociation, or subtraction. For example:

str a = "Hyper";
str b = "Script";
str c = a + b;        /* string concatenation, c contains "HS" */
str d = c - b;        /* string dissociation,  d contains "Hyper"       */
str e = c - a;        /* string dissociation,  e contains "Script"      */

_

Evaluation Order

Unary operators and assignment are right-associative. All others are left-associative. For example:

a = b = c;        /* means a = ( b = c ); */
x = a + b + c;        /* means x = ( a + b ) + c */

_

Precedence levels and associative rules stem from what we would expect from common usage. For example:

if ( i < 0 || i > max )        /* ... */

_

is equivalent to:

if ( ( i < 0 ) || ( i > max ) )        /* ... */

_

However, parenthesis should be used whenever these rules are in doubt.

The operators , (comma), && (logical and), and || (logical or) guarantee left to right evaluation. For example:

R = ( T = 1, T = T + 2 );

_

assigns 3 to both R and T.

In general, the order of evaluation of sub-expressions within an expression is undefined. That is, you cannot assume that expressions are evaluated left to right. For example:

int i = 1;
v[i] = i++;        /* undefined result */

_

could be evaluated as either v[1] = 1, or v[2] = 1.

Clone this wiki locally