Page 1 of 2

a[++y]=a[y]+1;

Posted: Wed Jan 28, 2004 10:08 am
by yiuyuho
Hello,

can anyone tell me the difference between

a[++y]=a[y]+1;

and

y++;
a[y]=a[y-1]+1;

????
it may be compiler specific, thx in advance

Posted: Wed Jan 28, 2004 10:26 am
by Dominik Michniewski
I think that it's compiler specific problem:

Main problem is:
when compiler evaluate right and left side of this expression ...
1. if compiler evaluate right side and after that left -> no difference
2. if compiler evaluate left side and after that right ... :-) no comments

We don't know which strategy will be taken by compiler, so best practice is use second solution :-)

Best regards
DM

Posted: Wed Jan 28, 2004 11:08 am
by yiuyuho
hi Dominik! are you just checking post around?

Thanks for your reply, I think I know what you're saying, but isn't '=' always right associative? that's the only way will x=y=z=t=5; work.

Posted: Wed Jan 28, 2004 11:36 am
by anupam
I have another problem when i use just like
printf("%d",cas++);
But i get ac. when i use
printf("%d",cas);
cas++;

I don't know why..

Posted: Wed Jan 28, 2004 12:51 pm
by Dominik Michniewski
anapum:
I always use your first example and I think, that I got Acc correctly :)

yiuyuho:
hahaha - no, I'm not checking topics around ;-) but sometimes I have more time - in other times I check board seldom ...
operator = is right associative , but in your case you have expressions on both sides of =. So compiler may (or not may) evaluate it in any way :)
i.e
E1 = E2
result of E2 would be used to set E1, but if E1 is expression too - what happened ? E1 will be evaluated first of E2 first ?

Best regards
DM

Posted: Wed Jan 28, 2004 1:00 pm
by Subeen
I think:
printf("%d", cas++);
and
printf("%d", cas); cas++;
are same.
but
printf("%d", ++cas); is different!

Re: a[++y]=a[y]+1;

Posted: Wed Jan 28, 2004 3:12 pm
by Ivor
yiuyuho wrote:a[++y]=a[y]+1;
Say y = 1.
++y => y = 2
and your code becomes: a[2] = a[2] + 1
yiuyuho wrote:y++;
a[y]=a[y-1]+1;
Same starting point, say y = 1.
y++ => y = 2;
and assignment becomes: a[2] = a[2 - 1] + 1, ie a[2] = a[1] + 1.

That's the difference. I haven't tested it but it is the way I have always understood C compiler. Shame, if I'm wrong. Usually if I'm not sure about the statement, I simply rewrite it. Better be clear.

Re: a[++y]=a[y]+1;

Posted: Wed Jan 28, 2004 3:34 pm
by CDiMa
yiuyuho wrote:Hello,

can anyone tell me the difference between

a[++y]=a[y]+1;

and

y++;
a[y]=a[y-1]+1;

????
it may be compiler specific, thx in advance
By the C standard the first assignment is undefined. That means that anything may happen, even that your program solves all problems in all ACM volumes at once ;)

Seriously, the ++ operator has a double meaning: it evaluates as an expression and has the side effect of changing the value of y. Side effects are guarateed to be complete at the next sequence point. Since in

Code: Select all

a[++y]=a[y-1]+1;
the sequence point is at the semicolon, you can't establish if the side effect has taken effect while accessing y to compute y-1. Furthermore if a variable is written to in an expression, all accesses to it within the same expression are allowed only to compute the value to be written. In your case you are using the the value of y to evaluate y-1 and not the value to be written to y.

Hope this clears things a little bit...

Ciao!!!

Claudio

Posted: Wed Jan 28, 2004 4:15 pm
by anupam
Subeen wrote:I think:
printf("%d", cas++);
and
printf("%d", cas); cas++;
are same.
but
printf("%d", ++cas); is different!
But subeen, I faced it in a problem names
monkey in a forest or like that..

you corrected my fault as i can remember. Again, I faced it during the conest of dhaka regional (Cyber Cafe, the problem)..
--
May be I am wrong.
or would you just check your previous posts??

Re: a[++y]=a[y]+1;

Posted: Wed Jan 28, 2004 4:54 pm
by CDiMa
Ivor wrote:
yiuyuho wrote:a[++y]=a[y]+1;
Say y = 1.
++y => y = 2
and your code becomes: a[2] = a[2] + 1
Not quite right...
++y results 2, value of y cannot be read for anything else until ';'
your code becomes: ?
Ivor wrote:That's the difference. I haven't tested it but it is the way I have always understood C compiler. Shame, if I'm wrong. Usually if I'm not sure about the statement, I simply rewrite it. Better be clear.
Sequence points are an obscure matter... better be clear ;)

Ciao!!!

Claudio

Posted: Wed Jan 28, 2004 6:33 pm
by little joey
i=1;printf("(%d,%d)",i++,i++);
can write (1,1) (1,2) (2,1) or even (2,2)
but i=1;(printf("%d",(i++,i++));
should write 2
:D

Posted: Wed Jan 28, 2004 7:04 pm
by CDiMa
little joey wrote:i=1;printf("(%d,%d)",i++,i++);
can write (1,1) (1,2) (2,1) or even (2,2)
but i=1;(printf("%d",(i++,i++));
should write 2
:D
i++,i++ isn't tricky and is well defined since the comma is a sequence point 8)

Ciao!!!

Claudio

Posted: Wed Jan 28, 2004 7:07 pm
by yiuyuho
Thank you very much for your explainations everyone, I guess it is complier specific.

I'll be more careful in the future about these kinds of stuff, thx a lot!

Re: a[++y]=a[y]+1;

Posted: Wed Jan 28, 2004 8:49 pm
by horape
yiuyuho wrote:Hello,

can anyone tell me the difference between

a[++y]=a[y]+1;

and

y++;
a[y]=a[y-1]+1;

????
it may be compiler specific, thx in advance
First one is invalid (unspecified/undefined really, don't know for sure), the order in which the ++y on the left side and the y on the right one are evaluated is not defined. You can not trust what will that code do.

More on http://www.eskimo.com/~scs/C-faq/q3.2.html

Saludos,
HoraPe

Posted: Thu Jan 29, 2004 12:12 am
by Farqaleet
Following is what MSDN says
"In the prefix form, the increment or decrement takes place before the value is used in expression evaluation..."
By this the increment is done before y is used anywhere in the expression. That's what microsoft says. but then again you can't say anthing otherwise without checking other docs eg. Borland, gcc, g++ etc.