Thread: Out of this program?How?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    45

    Out of this program?How?

    Code:
    main()
    {
    int i;
    for(i=1;i<=5;printf("\n%d",i));
    i++;
    }
    Actually I meant "output* of this program?How?"
    Why am I getting an indefinite loop of 1?
    Last edited by sameertelkar; 09-15-2012 at 03:58 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice the semi-colon at the end of the for loop. As such, your for loop has an empty body.

    Oh, and you should explicitly declare main as returning an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Because of the extra semi-colon your program is the same as
    Code:
    main()
    {
    int i;
    
    for(i=1;i<=5;printf("\n%d",i)) { /* do nothing */ }
    
    i++;
    }
    The for loop never ends: it's an infinite loop.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    45
    Thank you to you both.I think I got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM