Like Tree1Likes
  • 1 Post By qny

Out of this program?How?

This is a discussion on Out of this program?How? within the C Programming forums, part of the General Programming Boards category; 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 ...

  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 04:58 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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.
    sameertelkar likes this.

  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-17-2007, 12:43 AM
  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, 12:53 PM

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