Is an (empty) infinite loop undefined behavior in C?

前端 未结 2 1266
春和景丽
春和景丽 2020-12-16 01:25

Is an infinite loop like for (;;); undefined behavior in C? (It is for C++, but I don\'t know about C.)

2条回答
  •  Happy的楠姐
    2020-12-16 01:57

    The rationale for this question with relevance to C++ isn't relevant to C. Section 5.1.2.3p6 states the limits to optimisation, and one of them is:

    At program termination, all data written into files shall be identical to the result that execution of the program according to the abstract semantics would have produced.

    Now the question becomes "What data would execution according to the abstract semantics have produced?". Assuming a signal interrupts the loop, the program may very well terminate. The abstract semantics would have produced no output prior to that signal being raised, however. If anything, the compiler may optimise the puts("Hello"); away.

提交回复
热议问题