Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CONFUSED over OPERATOR PRECEDENCE

Status
Not open for further replies.

dakuneko

Programmer
Nov 17, 2000
19
SG
Assuming that a, b and c are of integer type whose initial value is 0:
Code:
	(1)  a = ++b + ++c;
	(2)  a = b++ + c++;
	(3)  a = ++b + c++;
	(4)  a = b-- + --c;

The result are as follows:
Code:
	(1) 2  
	(2) 2  
	(3) 5  
	(4) 5

Now I am confused. It seems that in (2) a = b++ + c++;, the + gets evaluated first before the ++.

How is this so? From what I've read, ++ has a higher precedence over the + then the = comes last.

Same goes with (3) and (4).

Can someone explain to me the reason?

Thanks!

Zagato
 
You're right, ++ has a higher precedence, but a++ and ++a mean totally different things:

a++ means evaluate a, and then increment it, so,
a++ + 4
if a = 3
is going to be 7,
and after the statement a will be 4.
This is because a is evaluated before it is incremented.

++a means increment a first, then evaluate it.
++a + 4
if a = 3
is going to be 8
and after the statement a will be 4.
This is because a is evaluated after it is incremented.

In the first example, a is considered a postfix operator because it comes after the value. In the 2nd example, it is a prefix operator. Ask again if you don't understand this. Also, this is c++ (I believe) not c, but no problem.

MWB As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Yeah, it is C++.

Your post helped me understand better. Thank you!

I understand what you mean about the postfix and prefix operators. But I partly, I am still confused. In the statement
Code:
        a = b++ + 4;

if I follow the rule of precedence below

HIGH PRECEDENCE
Symbol Name or Meaning Associativity
++ Post-increment Left to right
-- Post-decrement
++ Pre-increment Right to left
-- Pre-decrement
- Unary minus
+ Unary plus
+ Add Left to right
- Subtract
= Assignment Right to left
LOWEST PRECEDENCE

it seems that I would have to evaluate ++ (Post-increment) first then the + (Add) and finally the = (Assignment).

If I would follow the rule above, it seems that the value that I will get for a if b= 1 would be 6

(b is evaluated then incremented then added to 4 which would follow the precedence ++, + then =)

but if this was actually run and according to what you said (which is correct) the end result for a would be 5.

(b is evaluated then added to 4 then incremented which would be +, =, ++)

That is why I am confused about this.

Could the reason behind this is because of how the statement gets stored in memory and partly because of the way it gets parsed?

Obviously, I am confused about how it has been evaluated. It seemed to me that it violated the rule of precedence. But that's how it is. Maybe this is a special case.

So, maybe if you have time, could you explain to me why it has violated the precedence rule for this case?

Thanks again!

Zagato
 
I've got another one. Consider the code below

Code:
	/* function foo */
	int foo(int *ey)
	{
		*ey = 1;
		return 1;
	}

	/* main */
	int main(void)
	{

		int a = 0;	

		/* 1 */	  printf("Return value of ptrf is %d. The value of a \ 
		                  in this statement is %d\n", foo(&a), a);
		/* 2 */	  printf("The value of a is now %d\n", a);
		return 0;
	}

The statement /* 1 */ has evaluated the assignment of a for the printf before using the value assigned to a by the function foo.

The printf of /* 1 */ should produce a value 1 for the return and still 0 for a. But when it reaches /* 2 */, the value of a would be 1.

Somebody help! Where in the rule of precedence does this come from?

Thanks!

Zagato
 
No, in hte stmt
a = b++ + 4;

the order is:

Get the value for b, increment b, add *the value for b* and 4, assign it to a.

Conversly,

a = ++b + 4

is:

Increment b, get the value for b, add *the value for b* and 4, assign it to a

So, the precdence for ++ is higher than addition, but it just depends on when you lookup the value for b, one does it before the ++ and one after the ++. This is why you see the different answers.

MWB. As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top