Is the order of this operator C specified correctly?

The name is a bit vague as I don't know how to define this question.

It relates to the following code:

for (match = root, m_matchBase = match->requestedBase, m_matchLength = match->length; match != NULL; match = match->next, m_matchBase = match->requestedBase, m_matchLength = match->length) { if (m_matchBase <= m_base && m_matchBase + m_matchLength > m_base) break; // other stuff... } 

Are for loop statements guaranteed to execute sequentially?

For example, m_matchBase = match->requestedBase guaranteed to work after match = match->next ?

+7
c sequence-points
source share
4 answers

Yes, the comma operator (which is used here) will order the operations. This means that your loop is likely to work when match->next becomes null.

+8
source share

Expressions will be evaluated from left to right and after each assessment there will be a point in the sequence. In C, the grammar for the for statement without declaration from the standard section of the C99 project 6.8.5 operators:

for (expression opt ; expression opt ; expression opt ))

Thus , in each set of expressions there will be a comma , not just a separator, which means that assignments will be evaluated from left to right. This is described in section 6.5.17 Comma operator, which states:

The left operand of the comma operator is evaluated as a void expression; There is after his evaluation. Then the right operand is evaluated; the result has its type and meaning

Whether this is supported code is another question, it is important to note that when match>next returns NULL , you will invoke undefined behavior in subsequent subexpressions. This is probably a way to demonstrate that this is a bad choice in style, as it is easy to miss and difficult to verify in the current form.

+5
source share

Yes, see C ++ 11 standard (5.18):

A pair of expressions separated by a comma is evaluated from left to right; left expression is discarded, value expression

+3
source share

Yes.

The left operand of the comma operator is evaluated as a void expression; There is a point in the sequence between its evaluation and the point of the correct operand. Then the right operand is evaluated; The result has its type and meaning.

The && operator also has a sequence point.

+2
source share

All Articles