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

前端 未结 8 1974
自闭症患者
自闭症患者 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:16

    In C, true is implemented as follows (depending on compiler)

    #define TRUE 1
    

    or

    #define TRUE (-1)
    

    AND false is implemented as

    #define FALSE 0
    

    so while (1) is equivalent to while (true) since 0 is considered false.

    the while (1) == for (; ;) as there are no stopping condition.

    which is translated to assembler as

    :loop
      ...
      ...
      ...
      goto loop
    

    so if the assembler code doesn't have a ret or exit instruction, it's considered a infinite loop.

提交回复
热议问题