Topic : An introduction to C
Author : Tom Torfs
Page : << Previous 5  Next >>
Go to page :

negation: nonzero value -> 0, zero -> 1
~           Bitwise complement: all bits inverted
+           Unary plus
-           Unary minus
++          Pre/post-increment (see below)
--          Pre/post-decrement (see below)
(see 10. Arrays, strings and pointers)
*           Indirection
(see 10. Arrays, strings and pointers)
sizeof      Returns size of operand in bytes; two forms:
1) sizeof(type)
2) sizeof expression
3. Multi-       *           Multiply
plicative    /           Divide
%           Remainder (only works for integers)
-           Binary minus
5. Shift        <<          Shift bits left, e.g. 5 << 1 = 10
>>          Shift bits right, e.g. 6 >> 1 = 3
6. Relational   <           Less than
<=          Less than or equal to
>           Greater than
>=          Greater than or equal to (not =>)
7. Equality     ==          Equal to (not =)
!=          Not equal to
8. Bitwise AND  &           Bitwise AND, e.g. 5 & 3 == 1
9. Bitwise XOR  ^           Bitwise XOR, e.g. 5 ^ 3 == 6
10. Bitwise OR  |           Bitwise OR,  e.g. 5 | 3 == 7
11. Logical AND &&          Logical AND
12. Logical OR  ||          Logical OR
13. Conditional ?:          Conditional operator
(see 7.4 Using the ?: operator)
14. Assignment  =           Simple assignment
*=          Assign product (see below)
/=          Assign quotient
%=          Assign remainder
+=          Assign sum
-=          Assign difference
&=          Assign bitwise AND
^=          Assign bitwise XOR
|=          Assign bitwise OR
<<=         Assign left shift
>>=         Assign right shift
15. Comma       ,           Separates expressions

The precedence can be overruled by using parentheses. E.g. 5+4*3 would give 17, while (5+4)*3 would give 27.
The unary (2), conditional (13) and assignment (14) operators associate from right to left, the others from left to right. E.g. 4/2*3 groups as (4/2)*3, not 4/(2*3), but e.g. a = b = 7 groups as a = (b = 7), so you can assign a value to multiple variables in one line, but this reduces the clarity, at least in my opinion.
The left operand of the assignment operators must be what is called an "lvalue", i.o.w. something to which a value can be assigned. Of course you can't put a constant or an expression on the left side of an assignment operator, because they are not variables that have a location in which a value can be stored. For the same reason it cannot be, for example, the name of an array or string (see 10. Arrays, strings and pointers).
Don't worry if you're a bit overwhelmed by this list. It's normal if you don't understand most of them yet; the more advanced ones will be explained later in this document (see the forward references); the prefix/postfix and special assignment operators are explained below; the mathematical operators should be obvious, the bitwise operators too if you know something about binary arithmetic; just watch out for their precedence: they have higher precedence than the logical operators but lower precedence than the relational operators. For example:

a!=b&c

You may think the above expression performs a bitwise AND of b and c, and then compares this value to a for inequality. However, since the bitwise & operator has lower precedence than the equality operators, in fact a is compared to b for inequality, and the result of this comparison (1 or 0, see below) is then bitwise ANDed with c.
The logical AND/OR, relational and equality operators are usually used in conditions, such as those in an if statement (see Conditionals (if/else)). The relational and equality operators return 1 if the relationship or equality is true, 0 if it isn't. The logical AND/OR operators have the following logic:
logical AND: (&&)

left operand

Page : << Previous 5  Next >>