Thread: Difference?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Question Difference?

    What is the difference in this line:

    Code:
    while( c<10)
    {
    
      c++;
    }
    And

    Code:
    while (c<10)
    {
     ++c;
    }
    In the way they function? John

  2. #2
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    no difference

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    whitespace does not matter. It could just as easily be:
    Code:
    while   (c < 10 ) { ++
    c
    ;}
    though we'd have to castrate someone who writes it like that.
    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"

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I think the OP was asking about post-increment versus pre-increment, not about the whitespace.

    In this case, there is no practical difference. In other situations, it can make a difference.

    Consider:

    Code:
    int main(void)
    {
       int i=0;
       printf("Pre-increment: %d\t%d\n",++i,i);
       i=0;
       printf("Post-increment: %d\t%d\n",i++,i);
       return 0;
    }
    Last edited by KBriggs; 10-26-2010 at 11:42 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > printf("Pre-increment: %d\t%d\n",++i,i);
    In particular, in this situation, the result is undefined behaviour.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Is it? Woops, my mistake. Apologies to the OP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Replies: 10
    Last Post: 11-06-2005, 09:29 PM