Thread: looping question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    looping question

    Code:
    #include<stdio.h>
    int main()
    {
      int i;
      for (i = 0; i < 5; i++) {
        int i = 10;
        printf(" %d", i);
        i++;
      }
      return 0;
    }
    Output:10 10 10 10 10
    Here after i++ (after printf) value of i should be 11 and it should break out of loop after printing only once?? Why is the output like this then?
    Last edited by Salem; 10-31-2011 at 11:51 PM. Reason: remove pointless tags

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you declared a variable named i that is local to the loop body. This variable hides the variable named i that you declared prior to the loop.
    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
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're using 2 different variables called i. The loop condition is using the i that's outside the loop, and the printf() and everything else inside the loop is using the i that you declare inside the loop.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Thanks.. that sums it up.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ranjit89 View Post
    Thanks.. that sums it up.
    Ok, you have 4 concurrent threads with questions about garbage code...

    Exactly what are you trying to accomplish?

    If you are trying to learn C by quesswork, it ain't gonna happen... Get yourself a decent book or tutorial and work it page by page, example by example, beginning to end... At least then you'll have some clue how the language works.

    And as you were told in a different thread... you will not discover how the compiler works by continuously trying to go around it. There's an old story about teaching a pig to sing... in the end all you do is annoy the pig and waste your time.

  6. #6
    spaghetticode
    Guest
    Quote Originally Posted by CommonTater View Post
    There's an old story about teaching a pig to sing... in the end all you do is annoy the pig and waste your time.
    I've never heard of that before... it made me laugh. Still I'm glad living in times where wasting your time is even possible, plus an annoyed pig is still better than a slaughtered one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Me,, I have 9 Looping Question for C / C++
    By xcurialz in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2010, 01:53 AM
  2. Looping Question
    By ewandougie in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2009, 08:21 PM
  3. Program Looping question
    By BIt_toRreNt in forum C++ Programming
    Replies: 5
    Last Post: 02-11-2005, 12:14 AM
  4. Looping question
    By sparkyf in forum C Programming
    Replies: 2
    Last Post: 10-25-2003, 03:21 PM
  5. C++ looping question
    By deepthought in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2001, 01:33 PM