Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have said, the order in which this gets executed is deterministic. The <strong><em>operator precedence</em></strong> of the = operator guarantees that this is executed right-to-left. In other words, it guarantees that sample2 is given a value before sample1.</p> <p><em>However</em>, multiple assignments on one row is bad practice and banned by many coding standards (*). First of all, it is not particularly readable (or you wouldn't be asking this question). Second, it is dangerous. If we have for example</p> <pre><code>sample1 = func() + (sample2 = func()); </code></pre> <p>then operator precedence guarantees the same order of execution as before (+ has higher precedence than =, therefore the parenthesis). sample2 will get assigned a value before sample1. But unlike operator precedence, the <strong><em>order of evaluation of operators</em></strong> is not deterministic, it is unspecified behavior. We can't know that the right-most function call is evaluated before the left-most one.</p> <p>The compiler is free to translate the above to machine code like this:</p> <pre><code>int tmp1 = func(); int tmp2 = func(); sample2 = tmp2; sample1 = tmp1 + tmp2; </code></pre> <p>If the code depends on func() getting executed in a particular order, then we have created a nasty bug. It may work fine in one place of the program, but break in another part of the same program, even though the code is identical. Because the compiler is free to evaluate sub-expressions in any order it likes.</p> <hr> <p>(*) MISRA-C:2004 12.2, MISRA-C:2012 13.4, CERT-C EXP10-C.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload