Thread: inialisation for the while loop

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    inialisation for the while loop

    Hi folks.

    Is there a problem with nbr in the below sample code from my C book not being initialised? I am thinking that because of this, it could actually contain 99, so the program could actually stop before the user enters any values?

    Thanks in advance.

    Code:
    int value[5];
    int ctr = 0;
    int nbr;
    
    while(ctr < 5 && nbr != 99)
    {
        puts("Enter a number, 99 to quit");
        scanf("%d", &nbr);
        value[ctr] = nbr;
        ctr++;
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    When in doubt zero it out.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Yes, the loop acts on uninitialised output. This would lead to undefined behaviour (even if, most of the time, it would "work"). You wouldn't want to be on the end of that code if the loop did something interesting, like controlling a nuclear reactor.

    When a variable isn't initialised immediately on declaration, always be suspicious and audit its use to be sure that it gets initialised SOMEHOW before you do anything with it, no matter what code-path you end up taking.

    And most compilers will pick things like this up when they have warnings enabled.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    When you always want the loop body to execute at least once, then the appropriate type of loop to use is a do..while loop:
    Code:
     int value[5];
     int ctr = 0;
     int nbr = 0;
     do {
         puts("Enter a number, 99 to quit");
         scanf("%d ", &nbr);
         value[ctr++] = nbr;
     } while (ctr < sizeof(value)/sizeof(value[0]) && nbr != 99);
    Often this means that the initialisation is not necessary because it will be set to a value during the loop body. However in this case nbr might still not be given a value if the data entered by the user cannot be converted to a number. Thus initialising it to zero is still a good idea, or you could just check the return result of the scanf.
    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"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    When in doubt zero it out.
    Nah, when in doubt, find out what needs to be done, and do it.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 11:31 AM
  4. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 12:31 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM