# Some Basic questions

• 06-30-2009
BlaX
Some Basic questions
Code:

```Operators                                                    Associativity () [] -> .                                                          left to right ! ~ ++ -- + - * (type) sizeof                              right to left * / %                                                                left to right + -                                                                    left to right```

I was reading the 2nd chapter of K&R's The C programming language and came up accross this table.What i mentioned above is just a part of it.

I was confused by the 2nd ,3rd and 4th rows. As we can see in the second we have +-*
and in the 3rd we have * and in the 4th +-. Why the same thing is shown in different rows? Why is their Associativity different ?
And what is meant by associativity.Is it that if the same operator is available in the same expression ?

As i continued reading i came upon

x = f() + g();

where it said that the result is unpredictable.But wouldnt it go from left to right ? It should first it calculates the f() and then g() ... ?

Im confused with all of these ... Any help would be great .Thank you
• 06-30-2009
MK27
Quote:

Originally Posted by BlaX
I was confused by the 2nd ,3rd and 4th rows.

The 2nd row are the unary operators, which form an expression with only one other element. This includes post/pre-increment (eg, i++ or --i). So that's different than the math operators + and - in line 4. Unary * is dereference, not multiply.

The math operators are "binary", ie, they need two other elements to form an expression.
• 06-30-2009
laserlight
The result may well be predictable, but whether f() is evaluated first or g() is evaluated first is not predictable given just the source code.
• 06-30-2009
BEN10
So here goes my doubt. How's the +,- of 2nd row different from +,- of the 4th row. Can anybody give an example where they are different. How can +,- be used as unary operators?
• 06-30-2009
Salem
a = -1;
Unary minus

a = +1;
Unary plus
• 06-30-2009
laserlight
Quote:

Originally Posted by BEN10
How can +,- be used as unary operators?

By definition of "unary operator" and "left to right" associativity: +x and -x are such examples.
• 06-30-2009
tabstop
Or, even better:
a = -b; /* unary minus */
etc.
• 06-30-2009
BEN10
Ok, thanks to all. Now I got it.