Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
12.1 and for-loops
#1
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.
#2
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.
<t></t>
#3
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.
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 4 Guest(s)