C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 20th, 2004, 10:58 AM
nevillemehta nevillemehta is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: India
Posts: 64 nevillemehta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 44 m 17 sec
Reputation Power: 6
Postfix and Prefix....

Hi,

Here's a piece of code (C).

----------------------------------------
int y = 0, a = 0;
y = a++ + a++ + a++; (1st exp)
printf("Y: %d, A: %d\n",y,a);

y = 0, a = 0;
y = ++a + ++a + ++a; (2nd exp)
printf("Y: %d, A: %d\n",y,a);
----------------------------------------

Could some please explain me the 1st expression and 2nd expression.

------------
Output:
Y: 0, A: 3
Y: 7, A: 3
------------

PS: I'm most confused on how Y is 7 is the 2nd expression.

Thanks,
Neville

Reply With Quote
  #2  
Old September 20th, 2004, 01:30 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Haha, that is a puzzler. I stared at that for quite a while and couldn't figure it but a friend of mine shed some light on it.

It has to evaluate one part of the addition to start with (denoted by brackets below) so lets say it evaluates it as -

y = (++a + ++a) + ++a;

To do this it must first increment a twice (since ++variable increments the variable before any other evaluation happens so at this point a = 2. Reducing it to -

y = (2 + 2) + ++a;

So it increment a again so now a = 3 giving us -

y = (2 + 2) + 3;

y = 4 + 3;

y = 7;

Hope this helps,

-KM-

Reply With Quote
  #3  
Old November 17th, 2004, 10:12 AM
swangd swangd is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 1 swangd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Results differ on different compiler

I did it on the latest MS VC++:
Y=0, a=3
Y=9, a=3

On Linux using gc++ compiler:
Y=0, a=3
Y=7, a=3

I think logically, the 2nd Y should be 6.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Postfix and Prefix....


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway