简体   繁体   中英

Why does an int number in do (while number > 10) gives an endless loop?

In my understandings, the while should end the loop, cause int number "in" the loop should initialize it with 0. Or am i wrong? But it gives me an endless loop, printing zeroes. I compiled it with gcc and tried to debug it with gdb. It makes no sense for me, that the while doesn't stop the loop.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number = 20;
    do
    {
        int number = number / 2;
        printf("%d ", number);
    } while (number > 10);
}

You create new number every time in the loop, visible only within the loop and unrelated to the one outside the loop. Remove int in int number = number / 2;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM