GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Jan-2004, 15:03
Tony Tony is offline
New Member
 
Join Date: Jan 2004
Posts: 2
Tony is on a distinguished road

Newbie help with unary ops


What x = n++; would do is assign the value of n immediately to x, and then increment n by 1.
What x = ++n; would do is increment n by 1 immediately, and then assign that new value to x.

I'm still not grasping this fully. From what I gather the resulting x is going to be the same value either way, correct ? So then why would you need the two different ways (++n and n++) to achieving it ?

Can anyone explain this for me ?

Thanks,
Tony
  #2  
Old 04-Jan-2004, 16:51
cs2 cs2 is offline
Member
 
Join Date: May 2003
Location: California
Posts: 107
cs2 will become famous soon enough
Quote:
Originally Posted by Tony
What x = n++; would do is assign the value of n immediately to x, and then increment n by 1.
What x = ++n; would do is increment n by 1 immediately, and then assign that new value to x.
Your understanding is correct, but your conclusion is not. Perhaps it is easier if you think of these in real-life terms. Let's make 'n' a variable, with a value of "10".

So...
x = $n++ (post-increment operator)
This makes x=10 and $n=11

x = ++$n (pre-increment operator)
This makes x=11 and $n=11

Make sense?
__________________
The Whole Internet, LLC
Visit our Homepage, -or-
use our online CSS Editor
  #3  
Old 04-Jan-2004, 19:10
Tony Tony is offline
New Member
 
Join Date: Jan 2004
Posts: 2
Tony is on a distinguished road
Thanks CS2,

I understand how it's done now.

Tony
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Linux] Top newbie questions. JdS Computer Software Forum - Linux 3 22-Jan-2004 12:00
newbie help milwalt C++ Forum 1 27-Dec-2003 11:52
total newbie need help grunt123 Apache Web Server Forum 2 04-Dec-2003 11:14
newbie with stupid questions JUNK KED MySQL / PHP Forum 1 16-Oct-2003 08:58
Code problem (a newbie question) monkster87 C++ Forum 3 11-Aug-2003 12:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:05.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.