Thread: increment and decrement operators

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    1

    increment and decrement operators

    The increment and decrement operators can be placed before or after an integer variable. But I need to know the different behaviour of these two operators when they are placed before and after an integer variable, with examples

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: increment and decrement operators

    Originally posted by ee0u22ba
    The increment and decrement operators can be placed before or after an integer variable. But I need to know the different behaviour of these two operators when they are placed before and after an integer variable, with examples
    Try your C++ book on for size eh? Every C++ book covers this. It's exactly like it sounds:

    Before increments before the value is used.
    After increments afterwards.

    Example:

    output( c++ ); // passes thevalue of 'c' to output, then increments it

    output( ++c ); // increments 'c' and then passes it to output

    Not hard at all. Like I said, just look in any C++ book.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    This:
    Y = X++ ; // Y equals the old X value. Increment X

    Is the same as these two statements:
    Y = X ; // Y equals the old X value
    X = X + 1 ; // Increment X


    This:

    Y = ++X ; // Increment X. Y = the new value

    Is the same as these two statements:
    X = X + 1 ; // Increment X
    Y = X ; // Y = the new X value.


    And:
    cout << X++ ; // Prints the old value of X, then increments.
    cout >> ++X ; // Increments X, then prints the new value.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Actually, strictly speaking, I think it's more like:

    Value = X++;
    Code:
    int temp = X;
    X = X + 1;
    Value = temp;
    Whereas, Value = ++X;
    Code:
    X = X + 1;
    Value = X;
    Not 100% sure, but given that operators mostly if not always act as functions do, I doubt that they're smart enough to return a value and then increment it after (If you find a way of making a function that does this, please tell me! ). That's why I generally prefer ++X... I still could be wrong though, especially with those new-fangled compiler optimizations
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Why don't you just write a little program to figure this out yourself?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    I like to think of the postfix version as a sort of sidenote. eg...

    y = x++;

    ...would be "Y equals x. Oh, and increment x.", whereas

    y = ++x;

    ...would be "Y equals x incremented".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Does Increment Operators Work...??
    By ajayd in forum C Programming
    Replies: 37
    Last Post: 12-31-2008, 10:01 AM
  2. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  3. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM
  4. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  5. Replies: 11
    Last Post: 08-30-2004, 03:56 PM