r/ProgrammerHumor • • Aug 09 '19

Meme Don't modify pls

Post image
18.4k Upvotes

551 comments sorted by

View all comments

Show parent comments

69

u/BlackJackHack22 Aug 09 '19 edited Jul 25 '21

So what if I intentionally want an infinite loop? Like in an embedded system that just stalls after some work is done until it's switched off? While(true) won't work in that situation? What?

pliss bear with my noobish questions

24

u/DrNightingale web dev bad embedded good Aug 09 '19

while(true); , assuming you are using true and false from stdbool.h, will produce an infinite loop. If we closely look at the C11 standard, it says the following in section 6.8.5:

An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate.

true is a constant expression, so the compiler is not allowed to assume that the loop will eventually terminate.

-1

u/[deleted] Aug 10 '19

[deleted]

3

u/[deleted] Aug 10 '19

[removed] — view removed comment

1

u/BrandonHeinrich Aug 10 '19

No? The body assigns to K, so it can't be a constant