C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 25th, 2012, 03:01 PM
machupicchu machupicchu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 17 machupicchu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 13 sec
Reputation Power: 0
Types, Operators and Expression Code Example!

Hey guys,

I have this code and the output for this code is:

integer0 = 3, integer1 =5,integer2 = 9, integer3 =6.

I wanna understand why the integer1 is equal 5 and integer2 equal 9. The other makes sense.

Thank you very much.

g Code:
Original - g Code
    #include <stdio.h> #define ITERATIONS 3 void main(void) { int integer0 = 0, integer1 = 1, integer2 = 2, integer3 =3; for(;integer2++, integer0 < ITERATIONS ;) integer1 = (integer0++, ++integers2, integer3++); printf("integer0 = %1d, integer1 =%1d,"          "integer2 = %1d, integer3 =%1d.\n", integer0, integer1, integer2, integer3); }

Reply With Quote
  #2  
Old November 25th, 2012, 04:35 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
If I recall correctly, the comma operator has lowest precedence, evaluates from left to right, and has the value of the right hand expression.

Also, the value of the post-increment operator is the value of the variable. The variable changes afterward.

Therefor the final value of integer1 will be 1 less than the final value of integer3, which you say you understand.

integer2 starts at 2. You then increment it twice per cycle over three cycles bring it to 8. Bring your smart computer to the condition statement:

integer2++, integer0 < ITERATIONS;

integer2 increments once again, to 9, and integer0 is no longer less than ITERATIONS, so the condition is 0 and the loop terminates.

Don't write code like this.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old November 25th, 2012, 05:01 PM
machupicchu machupicchu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 17 machupicchu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 13 sec
Reputation Power: 0
Thank you b49P23TIvg!

But why integer2 inkrements once again, to 9 and integer1 to 5.

Why integer2 increments once again, to 9? And integer3 to 6??

Reply With Quote
  #4  
Old November 25th, 2012, 05:15 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,353 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 8 h 3 m 36 sec
Reputation Power: 383
My explanation has caused you to doubt your understanding of integer3, which you previously claimed to understand. Furthermore, you still don't understand integer1 and integer2. I quit.

Reply With Quote
  #5  
Old November 25th, 2012, 05:26 PM
machupicchu machupicchu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 17 machupicchu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 13 sec
Reputation Power: 0
You are right! I will try it tomorrow again...Thanx...!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Types, Operators and Expression Code Example!

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap