Like Tree1Likes
  • 1 Post By CommonTater

looping question

This is a discussion on looping question within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main() { int i; for (i = 0; i < 5; i++) { int i = 10; ...

  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
    21,594
    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.
    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
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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.
    dennis.cpp likes this.

  6. #6
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

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, 07:21 PM
  3. Program Looping question
    By BIt_toRreNt in forum C++ Programming
    Replies: 5
    Last Post: 02-10-2005, 11:14 PM
  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, 12:33 PM

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