Breaking out of a loop from within a function called in that loop

后端 未结 14 1024
盖世英雄少女心
盖世英雄少女心 2020-12-30 20:31

I\'m currently trying to figure out a way to break out of a for loop from within a function called in that loop. I\'m aware of the possibility to just have the

14条回答
  •  滥情空心
    2020-12-30 20:34

    Just set a global variable and check that on the loop:

    #include 
    
    int leave = 0;
    
    void foo (int a) {
        printf("a: %d", a);
        leave = 1;
    }
    
    int main(void) {
        for (int i = 0; i <= 100; i++) {
            foo(i);
            if (leave)
              break;
        }
        return 0;
    }
    

提交回复
热议问题