this post was submitted on 11 Nov 2025
569 points (97.5% liked)
Programmer Humor
27466 readers
2075 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
The compiler (in C) is allowed to assume that infinite loops eventually terminate. This can lead to these kinds of loops not actually running forever when built with an optimizing compiler.
ISO/IEC 9899:2017 §6.8.5 “Iteration statements”, paragraph 6:
“An iteration statement may be assumed by the implementation to terminate if its controlling expression is not a constant expression, and none of the following operations are performed in its body, controlling expression or (in the case of a for statement) its expression-3: – input/output operations – accessing a volatile object – synchronization or atomic operations."
It can, for example, simply optimize it away, assuming non-productive infinite loops are stupid and not reflective of what the code will actually do.
Pretty big caveat. If I'm reading this right
truedefinitely qualifies as a constant expression and the loop in the meme would therefore not be optimized away.There's also this part of the standard that throws a wrench into this hypothesis:
§5.1.2.3/4: (Program execution, Observable behavior):
So it seems that running forever isn't an observable property that must be preserved when code is transformed.
Still, I think compilers try to not surprise the developer too badly and would recognize a trivial loop most of the time.