What's the difference between var++ and ++var

This is a discussion on What's the difference between var++ and ++var within the C Programming forums, part of the General Programming Boards category; When you use for loops or a general variable incrementation, what difference does it make for the location of the ...

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    What's the difference between var++ and ++var

    When you use for loops or a general variable incrementation, what difference does it make for the location of the "++"? That is, what's the difference between these two for loop headers and the following variable adjustments:

    Code:
    for (example = 0; example < 1024; example++)
    for (example = 0; example < 1024; ++example)
    
    var++;
    ++var;
    If there is a difference, which would you use? If there is no difference, then what is the most common of the two?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    The only difference between prefix and postfix operators is the result of the operator. In postfix, a temporary object is created, the operation is applied, and the temporary is returned: the object before the operation is applied. In prefix, the operation is applied and the fixed object is returned, without the use of a temporary: the object after the operation is applied.

    Some people think that the temporary makes postfix a poor choice. This is not so if you use an optimizing compiler. But the main difference is what the operation yields as a result, because it can make a huge difference as part of a larger expression.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Code:
    x = 0;
    printf("X = %d\n" , x++);
    X = 0

    Code:
    x = 0;
    printf("X = %d\n" , ++x);
    X = 1
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by abachler View Post
    Code:
    x = 0;
    printf("X = %d\n" , x++);
    X = 0

    Code:
    x = 0;
    printf("X = %d\n" , ++x);
    X = 1
    That example is somewhat misleading. The for loop doesn't use the result of the increment expression. It just executes the expression for the side-effects, which of course aren't shown in both of those.

    However, whenever possible, use ++var, not var++. When you've learnt enough C++ as well, you'll see why.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    11

    Thumbs up

    u gotto know abt sequence points....'coz the value of a variable get updated between two sequence points..here is the link for sequence points

    http://msdn2.microsoft.com/en-us/library/ms861609.aspx

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    One more thing you should know also, when we use ' i++ ' then a temproray variable is created to store incremented value of ' i ', to assign it in i later on. while in case of ++i incremented value stores in ' i ' itself.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 08:51 PM
  2. compile error: storage size of 'var' isn't known
    By George2 in forum C Programming
    Replies: 9
    Last Post: 04-03-2007, 05:45 AM
  3. server question
    By xddxogm3 in forum Tech Board
    Replies: 24
    Last Post: 01-21-2004, 12:12 AM
  4. Void functions
    By cap in forum C Programming
    Replies: 16
    Last Post: 09-04-2002, 08:08 AM
  5. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21