A simple doubt in C !

Status
Not open for further replies.

truebs

Full Member level 5
Joined
Jan 21, 2005
Messages
310
Helped
22
Reputation
44
Reaction score
4
Trophy points
1,298
Location
Asia
Activity points
2,980
Hi,

We all know that for x=2

y = ++x ; y becomes 3
y = x++ ; y becomes 2 and x becomes 3 in both cases.

But as per rule of precendance... ++ operator has higher priority than = operator.
Then as per precendance .. y should become 3 in both the cases owing to higher precendance to ++ than =.

But it doesn't happen , why ? What is the possible bug or behavior of compiler ?

Thanks ..
 

"y = x++" means increment x after evaluating the expression.

So in this case, the expression is evaluated while x still equals 2, thus setting y to 2, and then after that evaluation is finished, x is incremented.
 

That's correct. Order of execution and precedence are two different, independent things.
 

I totally agree with lambtron and echo47, ++X and X++ are two different things.
Maybe if your code was:
++X;
X++;
Then there won’t be much difference but since you added an “=” to the expiration they will have two different effects.
 

hi,

in this statement:

x = *buf++;

means that x equals to *buf but for the next case buf points to next adress in memory.

if you write;

x = *(buf++);

x equals to the value in adress (buf+1),

that is the trick!
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…