When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto (in C)?

前端 未结 8 1960
自闭症患者
自闭症患者 2020-12-02 23:06

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto?

Thanks, Chenz

8条回答
  •  隐瞒了意图╮
    2020-12-02 23:30

    There is hardly any difference in generated assembly. It's more of an stylistic issue:

    Goto - just ooogly: jumps backward, no explicit infinite block

    while(1) - better, requires "dummy" condition though and you'll be often warned by compiler(warning level 4) or static analysis tool

    for(;;) might not be the prettiest, but imho fits best because this construct cannot have any other meaning (compared to while). But some other people prefer while(1) for the "same" reason...

提交回复
热议问题