C Programming (COP-2220)
This page lists C operators in order of precedence (highest to
lowest). Their associativity indicates in what order operators of equal
precedence in an expression are applied.
Operator
|
Description |
Associativity
|
()
[]
.
->
++ -- |
Parentheses (function call) (see Note 1)
Brackets (array subscript)
Member selection via object name
Member selection via pointer
Postfix increment/decrement (see Note 2) |
left-to-right
|
++
--
+
-
!
~
(type)
*
&
sizeof |
Prefix increment/decrement
Unary plus/minus
Logical negation/bitwise complement
Cast (change type)
Dereference
Address
Determine size in bytes |
right-to-left |
*
/
% |
Multiplication/division/modulus |
left-to-right |
+
- |
Addition/subtraction |
left-to-right |
<<
>> |
Bitwise shift left,
Bitwise shift right |
left-to-right |
<
<=
>
>= |
Relational less than/less than or equal to
Relational greater than/greater than or equal to |
left-to-right |
==
!= |
Relational is equal to/is not equal to |
left-to-right |
& |
Bitwise AND |
left-to-right |
^ |
Bitwise exclusive OR |
left-to-right |
| |
Bitwise inclusive OR |
left-to-right |
&& |
Logical AND |
left-to-right |
|| |
Logical OR |
left-to-right |
?: |
Ternary conditional |
right-to-left |
=
+=
-=
*=
/=
%=
&=
^=
|=
<<=
>>= |
Assignment
Addition/subtraction assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment |
right-to-left |
,
|
Comma (separate expressions) |
left-to-right |
- Note 1:
- Parentheses are also used to group
sub-expressions to force a different precedence;
such parenthetical expressions can be nested and are
evaluated from inner to outer.
- Note 2:
- Postfix increment/decrement have high
precedence, but the actual increment or decrement of the
operand is delayed (to be accomplished sometime before
the statement completes execution). So in the statement
y = x * z++;
the current value of
z is
used to evaluate the expression (i.e.,
z++ evaluates to
z)
and z
only incremented after all else is done. See
postinc.c
for another example.
- Demo Code
- 1 #include <stdio.h>
2 // although -- ++ is of higher predence than 3 // * and other operators, -- ++ will be executed at last. 4 // following one is a nice demo to prove this. 5 6 int main() 7 { 8 int i[] = {3, 5}; 9 int *p = i; 10 11 // first, *p, then --, last, ++; now, j is 2 and p is pointing to 5. 12 int j = --*p++; 13 printf("*p == %d\n", *p); 14 15 // first, *, then ++; now j is 5; 16 j = j * j++; 17 18 printf("j == %d\n\n", j); 19 return 0; 20 } - Output:
- [root@localhost tmp]# g++ xxx.cpp && a.out
*p == 5 j == 5
[root@localhost tmp]#
|
The operators at the top of this list are evaluated first. Operators within a group have the same precedence. All operators have left-to-right associativity unless otherwise noted.
Precedence | Operator | Description | Example | Overloadable | Associativity |
1 |
:: |
Scope resolution operator |
Class::age = 2; |
no |
left to right |
2 |
() |
Function call |
printf(“Hello world\n”); |
yes |
left to right |
() |
Member initalization |
c_tor(int x, int y) : _x(x), _y(y * 10) {} |
yes |
[] |
Array access |
array[4] = 2; |
yes |
-> |
Member access from a pointer |
ptr->age = 34; |
yes |
. |
Member access from an object |
obj.age = 34; |
no |
++ |
Post-increment |
for (int i = 0; i < 10; i++) cout << i; |
yes |
-- |
Post-decrement |
for (int i = 10; i > 0; i--) cout << i; |
yes |
dynamic_cast |
Runtime-checked type conversion |
Y& y = dynamic_cast<Y&>(x); |
no |
static_cast |
Unchecked type conversion |
Y& y = static_cast<Y&>(x); |
no |
reinterpret_cast |
Reinterpreting type conversion |
int const* p = reinterpret_cast<int const*>(0x1234); |
no |
const_cast |
Cast away/Add constness |
int* q = const_cast<int*>(p); |
no |
typeid |
Get type information |
std::type_info const& t = typeid(x); |
no |
3 |
! |
Logical negation |
if (!done) ... |
yes |
right to left |
not |
Alternate spelling for ! |
~ |
Bitwise complement |
flags = ~flags; |
yes |
compl |
Alternate spelling for ~ |
++ |
Pre-increment |
for (i = 0; i < 10; ++i) cout << i; |
yes |
-- |
Pre-decrement |
for (i = 10; i > 0; --i) cout << i; |
yes |
- |
Unary minus |
int i = -1; |
yes |
+ |
Unary plus |
int i = +1; |
yes |
* |
Dereference |
int data = *intPtr; |
yes |
& |
Address of |
int *intPtr = &data; |
yes |
sizeof |
Size (of the type) of the operand in bytes |
size_t s = sizeof(int); |
no |
new |
Dynamic memory allocation |
long* pVar = new long; |
yes |
new [] |
Dynamic memory allocation of array |
long* array = new long[20]; |
yes |
delete |
Deallocating the memory |
delete pVar; |
yes |
delete [] |
Deallocating the memory of array |
delete [] array; |
yes |
(type) |
Cast to a given type |
int i = (int)floatNum; |
yes |
4 |
->* |
Member pointer selector |
ptr->*var = 24; |
yes |
left to right |
.* |
Member object selector |
obj.*var = 24; |
no |
5 |
* |
Multiplication |
int i = 2 * 4; |
yes |
left to right |
/ |
Division |
float f = 10.0 / 3.0; |
yes |
% |
Modulus |
int rem = 4 % 3; |
yes |
6 |
+ |
Addition |
int i = 2 + 3; |
yes |
left to right |
- |
Subtraction |
int i = 5 - 1; |
yes |
7 |
<< |
Bitwise shift left |
int flags = 33 << 1; |
yes |
left to right |
>> |
Bitwise shift right |
int flags = 33 >> 1; |
yes |
8 |
< |
Comparison less-than |
if (i < 42) ... |
yes |
left to right |
<= |
Comparison less-than-or-equal-to |
if (i <= 42) ... |
yes |
> |
Comparison greater-than |
if (i > 42) ... |
yes |
>= |
Comparison greater-than-or-equal-to |
if (i >= 42) ... |
yes |
9 |
== |
Comparison equal-to |
if (i == 42) ... |
yes |
left to right |
eq |
Alternate spelling for == |
!= |
Comparison not-equal-to |
if (i != 42) ... |
yes |
not_eq |
Alternate spelling for != |
10 |
& |
Bitwise AND |
flags = flags & 42; |
yes |
left to right |
bitand |
Alternate spelling for & |
11 |
^ |
Bitwise exclusive OR (XOR) |
flags = flags ^ 42; |
yes |
left to right |
xor |
Alternate spelling for ^ |
12 |
| |
Bitwise inclusive (normal) OR |
flags = flags | 42; |
yes |
left to right |
bitor |
Alternate spelling for | |
13 |
&& |
Logical AND |
if (conditionA && conditionB) ... |
yes |
left to right |
and |
Alternate spelling for && |
14 |
|| |
Logical OR |
if (conditionA || conditionB) ... |
yes |
left to right |
or |
Alternate spelling for || |
15 |
? : |
Ternary conditional (if-then-else) |
int i = a > b ? a : b; |
no |
right to left |
16 |
= |
Assignment operator |
int a = b; |
yes |
right to left |
+= |
Increment and assign |
a += 3; |
yes |
-= |
Decrement and assign |
b -= 4; |
yes |
*= |
Multiply and assign |
a *= 5; |
yes |
/= |
Divide and assign |
a /= 2; |
yes |
%= |
Modulo and assign |
a %= 3; |
yes |
&= |
Bitwise AND and assign |
flags &= new_flags; |
yes |
and_eq |
Alternate spelling for &= |
^= |
Bitwise exclusive or (XOR) and assign |
flags ^= new_flags; |
yes |
xor_eq |
Alternate spelling for ^= |
|= |
Bitwise normal OR and assign |
flags |= new_flags; |
yes |
or_eq |
Alternate spelling for |= |
<<= |
Bitwise shift left and assign |
flags <<= 2; |
yes |
>>= |
Bitwise shift right and assign |
flags >>= 2; |
yes |
17 |
throw |
throw exception |
throw EClass(“Message”); |
no |
|
18 |
, |
Sequential evaluation operator |
for (i = 0, j = 0; i < 10; i++, j++) ... |
yes |
left to right |
One important aspect of C++ that is related to operator precedence, is the order of evaluation and the order of side effects in expressions.
In most circumstances, the order in which things happen is not specified.
For example in f() + g()
whether f()
or g()
is called first is not specified.
If at least one of the functions has side effects the results may differ across compilers, different versions of the same compiler or even between multiple runs of the same compiler.
Further, the effect of certain expressions is undefined.
For example, consider the following code:
float x = 1;
x = x / ++x;
The value of x and the rest of the behaviour of the program after evaluating this expression is undefined.
The program is semantically ill-formed:
x is modified twice between two consecutive sequence points.
Expressions like the one above must be avoided.
When in doubt, break a large expression into multiple statements to ensure that the order of evaluation is
correct.
Overloading of operators can be very useful and very dangerous.
On one hand overloading operators for a class you have created can help with logistics and
readability of code.
On the other hand you can overload an operator in such a way that it can either obfuscate or just downright break your program.
Use carefully.
In particular never overload &&
, ||
or ,
.
In the overloaded context they lose the guarantee that the left operand is evaluated before the second and that there is a sequence point inbetween.
There are two ways to over load an operator: global function or class member.
Example of overloading with a global function:
ostream& operator <<(ostream& os, const myClass& rhs);
But to be able to reach any private data within a user defined class you have to
declare the global function as a friend within the definition of the class.
Example:
class myClass {
// Gives the operator << function access to 'myData'
// (this declaration should not go in public, private or protected)
friend ostream& operator <<(ostream& lhs, const myClass& rhs);
private:
int myData;
}
Overloading with a class member can be done as follows:
class myClass {
public:
// The left hand side of this operator becomes '*this'.
int operator +(const myClass& rhs);
private:
int myData;
}
Integer
----------------------------------
size of signed type is equal to unsigned type
2 [signed] short [int]
2 unsigned short [int]
4 [singed] int
4 unsigned [int]
4 [signed] long [int]
4 unsigned long [int]
8 [signed] long long [int]
8 unsigned long long [int]
Real Number (Float Point Number)
----------------------------------
the number must has "." char.
you can't use signed/unsigned with real number.
4 float
8 double
12 long double (rarely used)
bit operation
-----------------------------------
int i = 9 / 4; // int i = 9 >> 2;
int j = 9 * 4; // int j = 9 << 2;
int k = 9 * 5; // int k = 9 * (4 + 1); // int k = (9 << 2) + 9;
Print
-----------------------------------
\b \r
1 #include <stdio.h>
2
3 int main()
4 {
5 printf("a\bb\n");
6 printf("a\bbc\bbde\n");
7 printf("a\bbc\bbde\rf\n");
8 printf("abcde\rfghijk\n");
9 return 0;
10 }
output:
[root@localhost tmp]# gcc printf.c && a.out
b
bbde
fbde
fghijk
outcome:
a\bc ---> c
a\bc\bd ---> (a\bc)\bd ---> c\bd ---> d
a\rb ---> b
ab\rc ---> cb
abc\rd ---> dbc
abc\rdefg ---> defg
Input problems
1.
Use the correct conversion specifier for the declared data type in a scanf(). A float is %f, an int is %d, and a double is %lf (lower case L). If you declare a variable to be an int, don't try to input it with a %f specifier.
2.
Don't use a precision specification in a scanf() conversion specifier. For example, scanf("%.2f", &payRate) will not work (at least the ".2" part won't). Use scanf("%f", &payRate) instead. Width specifiers can be used, however.
Demo Code: 1 #include <stdio.h> 2 3 int main(int argc, char* argv[]) 4 { 5 float var; 6 scanf("%.2f", &var); 7 printf("%.2f\n", var); 8 return 0; 9 }
Output:[root@localhost tmp]# g++ xxx.cpp && a.out3.330.00[root@localhost tmp]#
3.
Use the addressing operator (&) for your variables used as targets for input with scanf()'s, unless the target is already an address (such as the name of an array or a pointer variable).
4.
The scanf() function can't be used with the %s specifier to input a string that contains white space (like "Electric sander"). Use another function, like gets().
5.
When using getchar() or scanf() with a %c (character) conversion specifier, be aware that the last input operation (like a previous scanf() or getchar()) may have left characters in the keyboard buffer.