a[++y]=a[y]+1;
Moderator: Board moderators
a[++y]=a[y]+1;
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
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
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
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
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 ...

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

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
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
I always use your first example and I think, that I got Acc correctly

yiuyuho:
hahaha - no, I'm not checking topics around

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
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
Re: a[++y]=a[y]+1;
Say y = 1.yiuyuho wrote:a[++y]=a[y]+1;
++y => y = 2
and your code becomes: a[2] = a[2] + 1
Same starting point, say y = 1.yiuyuho wrote:y++;
a[y]=a[y-1]+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.
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
Re: a[++y]=a[y]+1;
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 onceyiuyuho 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

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;
Hope this clears things a little bit...
Ciao!!!
Claudio
But subeen, I faced it in a problem namesSubeen wrote:I think:
printf("%d", cas++);
and
printf("%d", cas); cas++;
are same.
but
printf("%d", ++cas); is different!
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??
"Everything should be made simple, but not always simpler"
Re: a[++y]=a[y]+1;
Not quite right...Ivor wrote:Say y = 1.yiuyuho wrote:a[++y]=a[y]+1;
++y => y = 2
and your code becomes: a[2] = a[2] + 1
++y results 2, value of y cannot be read for anything else until ';'
your code becomes: ?
Sequence points are an obscure matter... better be clearIvor 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.

Ciao!!!
Claudio
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
Re: a[++y]=a[y]+1;
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.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
More on http://www.eskimo.com/~scs/C-faq/q3.2.html
Saludos,
HoraPe
Following is what MSDN says
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."In the prefix form, the increment or decrement takes place before the value is used in expression evaluation..."