Operators join smaller expressions to form larger expressions. Just as a function performs some function with its arguments (if any), operators operate on operands (if any). However while functions have some nice syntax like printf("hello"), operators have their own syntax.

Here are the operators in order of precedence (highest to low).

Precedence Example Description C C++ Java C# JavaScript
Scope ::NUM_ELEMENTS Unary scope resolution for globals. n y      
std::cout Binary scope resolution for class and namespace members. n y      
Postfix swap(x,y) Function call. y y y y y
arr[7] Array subscript/index y y y y y
object.member Element selection or member access, identifier. For object oriented programming. No overloading. y y y y y
ptr->member equivalent to (*ptr).member Element selection or member access, pointer y y     n
typeid(std::cout) Run time type ID for an object. n y      
typeid(std::iostream) Run time type ID for a type. n y      
static_cast<float>(i) Compile-time type conversion, cast operator. n y      
dynamic_cast<std::istream>(stream) RTTI type conversion, cast operator. n y      
const_cast<char*>("Hello") Removes the const qualifier from a pointer or a reference, cast operator. n y      
reinterpret_cast<const long*>("Hello") Interprets raw data as being of some other type, cast operator. n y      
int(myFloat) Functional cast operator. n y   y  
std::string("Hello",0,5) Constructor call for creating a temporary object, esp. of a class type n y      
(R-to-L)
Unary
b=++a is the same as a=a+1; b=a;. Unary increment before use.
y y y y y
b=--a is the same as a=a-1; b=a;. Unary decrement before use. y y y y y
b=a++ is the same as b=a; a=a+1;. Unary increment after use. y y y y y
b=a-- is the same as b=a; a=a-1;. Unary decrement after use. y y y y y
+i Unary plus. y y y y y
-i Unary minus. Changes the sign. y y y y y
!isTrue yields false. Logical NOT. y y y y y
~mask Bitwise NOT or zero's complement y y y y y
i = (int)f Cast. Converts the variable to the datatype in parentheses. DEPRECATED IN C++. y y y y  
i = *pi Dereference. "pointer". Gets the value of a reference. y y      
int *pi = &i Reference. "address of". Makes a reference/pointer. y y      
sizeof 123 Size. Gets the size in bytes of expressions. No overloading. y y      
sizeof(int) Size. Gets the size in bytes of types. No overloading. y y      
new int New, instantiates object, single. n y y y  
new int[9] New, instantiates object, dynamically allocated array. n y      
new (arg1) int New, instantiates object, single, with arguments. n y      
new (arg1) int[9] New, instantiates object, dynamically allocated array, with arguments. n y      
delete ptr Delete, frees memory of pointer. n y      
delete [] arr Delete, frees memory of dynamically allocated array. n y      
(R-to-L)
Member pointer
obj.*memptr Member pointer of an object of class/union type or a referemce to it. n y      
ptr->*memptr Member pointer of a pointer to an object of class/union type. n y      
Multiplicative 5*2 yields 10. Multiplication. y y y y y
5/2 yields 2.5. Division. y y y y y
5%2 yields 1. Modulus gives the remainder of a division of two integer values. y y y y y
Additive 5+2 yields 7. Addition. y y y y y
5-2 yields 3. Subtraction.   y y y y
Bitwise shift 9<<2 yields 36 since 1001 becomes 100100. Bitwise left shift. Shift binary a to the left b places and pad the right side with b 0s. y y y y y
9>>2 yields 2 because 1001 becomes 10. Bitwise right shift. Shift binary a to the right b places and discard the b rightmost digits. y y y y y
9>>>2 yields 2 because 1001 becomes 10. Bitwise right shift and pad the left with b 0s. Shift binary a to the right b places and discard the b rightmost digits then pad the left with 0s. For non-negatives, >> and >>> yield the same results. n n y n y
Relational inequality a<a yields false. Relational less than. Returns true if true. y y y y y
a<=a yields true. Relational less than or equal to. Returns true if true. y y y y y
a>a yields false Relational greater than. Returns true if true. y y y y y
a>=a yields true Relational greater than or equal to. Returns true if true. y y y y y
Relational equality a==a yields true Comparison equal to. y y y y y
a!=a yields false Comparison not equal to. y y y y y
  10&12 yields 8, since 1010 and 1100 yields 1000. Bitwise AND. Returns 1s for bit positions where both operands are 1. y y y y y
  10^12 yields 6, since 1010 and 1100 yields 0110. Bitwise XOR (exclusive or). Returns 1s for bit positions where either operand is 1 but not both. y y y y y
  10|12 yields 14, since 1010 and 1100 yields 1110. Bitwise OR (inclusive or). Returns 1s for bit positions where either operand is 1. y y y y y
  a&&b Logical AND y y y y y
  a||b Logical OR y y y y y
(R-to-L) (a>b)?a:b yields the greater of a or b. Ternary conditional. Given c?t:f, if c is true, then return t, else return f. No overloading. y y y y y
(R-to-L)
Assignment
a=b
Direct assignment. EG: a = 1 + (b=2). EG: a=b=c=3. y y y y y
a+=b is the same as a = a+b.

Additive assignment.

y y y y y
a-=b is the same as a = a-b. Subtractive assignment. y y y y y
a*=b is the same as a = a*b. Multiplicative assignment. y y y y y
a/=b is the same as a = a/b. Divisive assignment. y y y y y
a%=b is the same as a = a%b. Modulus assignment. y y y y y
a<<=b is the same as a = a<<b. Bitwise shift assignment y y y y y
a>>=b is the same as a = a>>b. Bitwise shift assignment y y y y y
a>>>=b is the same as a = a>>>b. Padding bitwise shift assignment n n y n y
a&=b is the same as a = a&b. Bitwise AND assignment y y y y y
a^=b is the same as a = a^b. Bitwise XOR assignment y y y y y
a|=b is the same as a = a|b. Bitwise OR assignment y y y y y
  throw "index out of bounds" Throw exception n y      
  while(x=func(1),y+=3,z<9)
for (var i=0, j=9; i <= 9; i++, j--)
  document.writeln("a["+i+","+j+"]= " + a[i,j])
Comma operator. Evaluate a list of expressions but return the last one. y y   y y

Notes by Language

Java

Additional Java operators:

C#

Additional C# operators:

JavaScript

Page Modified: (Hand noted: 2007-08-06 01:47:19Z) (Auto noted: 2007-11-17 06:48:47Z)