theldoria
theldoria

Reputation: 378

Is loop in C guaranteed to be executed in order?

Is 'for' loop in ANSI-C guaranteed to be executed in order, or can it be optimized by compiler using out-of-order execution? If so, which compilers do that? In which optimization level? How can this optimization be prevented?

Upvotes: 0

Views: 189

Answers (1)

ouah
ouah

Reputation: 145899

The compiler has the right to execute statements out of order if it is able to determine it does not change the observable behavior of the program. Use volatile objects if you want to execute statements strictly according to the rules of C abstract machine.

Upvotes: 3

Related Questions