0

Why does the following code loop 5000 times instead of the 5 times I expect?

int height = 5;

for (int height; height > 0; height -= 1){
    printf('Something')  }
}
jwpfox
  • 5,124
  • 11
  • 45
  • 42
james jake
  • 39
  • 1
  • 8
  • 4
    Just remove the in-loop declaration altogether: `for (; height > 0; height--)` will work. – Tom Karzes Nov 17 '17 at 11:05
  • @TomKarzes; In some context you may need to retain the value of `height` in the outer scope. In that case you have to use another variable for looping. – haccks Nov 17 '17 at 11:09
  • 2
    answer: *You don't.* Understanding the basic syntax and clauses of `for` would make this clear. Also, it's more idiomatic and readable to write `--height` instead of `height -= 1`. _[insert obligatory OT rant about not using post inc/decrement unless you need them]_ – underscore_d Nov 17 '17 at 11:09
  • Some questions like these have 10 downvotes in a minute. Others have an answer with (currently) three upvotes... –  Nov 18 '17 at 00:45

4 Answers4

3

but when I run it, it does not iterate through the loop 5 times but rather something like 5000.

That's because height declared in the for loop shadows the one declared outside. So you are effectively using uninitialized height, which is potentially undefined behaviour.

You can leave out the declaration in order to use the previously declared value:

int height = 5;

for (; height > 0; height -= 1) {
    printf("Something");
}

If you don't want to change height, you can use a temporary:

int height = 5;

for (int x = height; x > 0; x -= 1) {
    printf("Something");
}

which would leave height unchanged.

Also note that values in single quotes are multi-byte chars and are not strings. So you can't pass 'Something' to printf.

P.P
  • 117,907
  • 20
  • 175
  • 238
1

Why do you have to redefine variables in a for loop in C

That may be because of the fact that you want to preserve/use the value of variable after the loop.

In case of

int height = 5;

for (int h = height; h > 0; h--){
    printf('Something')  }
}

height will have value 5. While in case of

int height = 5;

for (height; height > 0; height--){
    printf('Something')  }
}

height will have value zero after the for loop.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    Of course, the `height` in the first `for` clause there doesn't achieve anything, and may make readers of your code think you meant to do something else. IMO it's better to omit it. – underscore_d Nov 17 '17 at 11:10
  • @underscore_d; Apart from that there were few other bugs int the earlier code snippets. I have fixed all after multiple edits :) – haccks Nov 17 '17 at 11:25
  • Oh, I mean the `for (height;` that's now in the final excerpt - not sure why I said "first" - but I see what you mean about the other ones. :P – underscore_d Nov 17 '17 at 11:26
  • @TonyTannous; I guess there is a check in `for` loop which ensures that `height` must not go below `0`. – haccks Nov 17 '17 at 11:34
0

This is not what redefining originally was.

Basically, because C have scopes, redefining a variable hides all variables from outer scopes:

for (int i=0; i<10; i++)
    for (int i=0; i<10; i++) {
        // Code that'll be looped 100 times
        // Code here can't access the outer `i`
    }

If you don't redefine it

for (int i=0; i<10; i++){
    for (; i<10; i++)
        // Code that'll be looped 10 times
    // Code that'll be run only once
}

If you define a variable without initializing it:

for (int h; h>0; h--)
    // Code that'll be looped for unknown times

In this code, the initial value of h is indeterminate. It could be 5000 or zero, or even more than 2,000,000,000! Always assign a value to a variable before using it.

iBug
  • 35,554
  • 7
  • 89
  • 134
-1

You have to redefine height variable with user input in your loop definition.

You can do it like this:

int user_input;
scanf("%d", &user_input);

for (int height = user_input; height > 0; height -= 1){
    printf("height = %d\n", height);  
}

If user enters "5", this code will print this:

height = 5
height = 4
height = 3
height = 2
height = 1
  • Oops, of course. Still, I presume they already know how to do that, and it wasn't the main thing they were asking about. You don't really answer the question about redefining or reusing the variable, *once* the user has supplied it. Saying _"You have to redefine"_ isn't true, as everyone else has shown. – underscore_d Nov 17 '17 at 11:27
  • "I want to get the variable as input from the user." - author of the question said about user input. I just wrote an example with user input. – A. Zhabolenko Nov 17 '17 at 11:28
  • "I presume they already know how to do that" - i wrote my answer when it was no other answers. Further i edited it and it changed position in answers list. I am new here, sorry. – A. Zhabolenko Nov 17 '17 at 11:32