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

Write here if you have problems with your C++ source code

Moderator: Board moderators

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

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

Post 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
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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
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)
yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post 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.
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post 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..
"Everything should be made simple, but not always simpler"
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post 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
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)
Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen »

I think:
printf("%d", cas++);
and
printf("%d", cas); cas++;
are same.
but
printf("%d", ++cas); is different!
Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

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

Post 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.
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.
CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

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

Post 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
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post 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??
"Everything should be made simple, but not always simpler"
CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

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

Post 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
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post 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
CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Post 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
yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post 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!
horape
New poster
Posts: 49
Joined: Sat Sep 13, 2003 3:18 pm
Location: Buenos Aires

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

Post 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
Farqaleet
New poster
Posts: 15
Joined: Wed Jan 28, 2004 11:24 pm

Post 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.
Post Reply

Return to “C++”