MISRA Discussion Forums

Full Version: 12.1 and for-loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Given the code:
Code:
void f()
    {
    int i, j;
    for( i =0, j = 0; i < 10; i++, j++ ) {}
    }
does rule 12.1 'advise' (instead of 'require') I place '()' around the for-loop initializers? In other words, to what extent does this rule apply in the head of a for-loop? The precedence relied on is the precedence of the '=' operator with respect to the ',' operator.
I'm not sure if I understand the question, since I see no case in that example where the code relies on operator precedence.

However, note that rule 12.10 (req) forbids the comma operator.
Note, use of the comma operator is banned under Rule 12.10.

If no reliance is being placed on C's operator precedence then the loop should be written:

Code:
for ((i = 0), (j = 0); i < 10; i++, j++) {}

Rule 12.1 recognises that it would clutter code considerably if every expression were parenthesised. It leaves the matter of what constitutes "too much" clutter up to individual or organisational choice.