MISRA Discussion Forums
12.1 and for-loops - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.12 Expressions (https://forum.misra.org.uk/forumdisplay.php?fid=39)
+---- Thread: 12.1 and for-loops (/showthread.php?tid=516)



12.1 and for-loops - gs - 14-05-2008

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.


Re: 12.1 and for-loops - Lundin - 15-05-2008

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.


Re: 12.1 and for-loops - misra-c - 20-05-2008

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.