Like Tree4Likes
  • 2 Post By whiteflags
  • 1 Post By ledow
  • 1 Post By iMalc

inialisation for the while loop

This is a discussion on inialisation for the while loop within the C Programming forums, part of the General Programming Boards category; Hi folks. Is there a problem with nbr in the below sample code from my C book not being initialised? ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    When in doubt zero it out.
    dmh2000 and cfanatic like this.

  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.
    cfanatic likes this.

    - 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,292
    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.
    cfanatic likes this.
    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
    21,397
    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.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 06: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, 10: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

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