Thread: ++i vs i++; what's the diff??

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    ++i vs i++; what's the diff??

    I am confused on the logic of expressions i++ and ++i and --i

    [code]
    int i, j, k;

    i=3, j=4, k=5;
    printf("%d ", i++ - j++ + --k); //this one is the confusing part//
    printf("%d %d %d, i, j, k); // the answr depndt on 1st part//

    [\code]

    the above yields following output:

    3 4 5 4


    Why??

    first integer printed = 3; why??

    i= 3 j=4 k=5
    i++ I am thinking i = 3+1 =4
    j++ I am thinking j = 4 + 1 = 5
    --k I am thinking k = -1 + 5 = 4

    So with these assumptions:
    i++ - j++ + --k =
    4 - 5 + 4 = -5
    but when I input code and compile; I get 3 HOW ??
    Sue B.

    dazed and confused


  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    the difference between ++i and i++ depends on whether or not you are passing on to another expression.

    for instance....

    int i=0;
    i++;
    is the same as
    int i=0;
    ++i;

    but when they combine with other expressions this happens...

    ++i is called prefix increment. This means 1 is added to i and then that value is passed on.
    i++ is called postfix increment.This means the value of i is passed on and then i is incremented.

    -- works the same.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    need more explanation

    How about evaluating the first of the samples I gave
    so I understand how i++ postincrements and ++i
    preincrements

    also --i ; is this subtracting 1 before passing i???
    Sue B.

    dazed and confused


  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok here:-
    printf("%d ", i++ - j++ + --k);

    lets say i is 5 j is 3 and k is 2

    i++ is pass 5 then increment i to 6
    - j++ subtract 3 from 5 then increment j to 4
    +--k subtract 1 from k then add so this evalutes to +(2-1)

    so the whole expression 5-3+1=3
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    thanks

    I get it now.

    Wish the textbook explained as well as you did, Stone.
    Sue B.

    dazed and confused


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. diff parser
    By ngloosh in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 12:19 PM
  2. Diff bet fopen() & open()
    By ankurcse in forum C Programming
    Replies: 2
    Last Post: 05-24-2006, 09:40 AM
  3. why does this code do something diff on 2 diff boxes?
    By crypto_quixote in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 12:11 PM
  4. whats the diff?
    By Moffesto in forum C# Programming
    Replies: 2
    Last Post: 06-19-2002, 02:23 AM
  5. Diff compiler == diff program????
    By Imperito in forum C++ Programming
    Replies: 21
    Last Post: 04-25-2002, 12:50 PM