Both versions are unreadable, because of the following problems:
The 3 expressions that make a for loop should be kept simple (1):
- The first expression should only be concerned with the declaration/initialization of the loop iterator. There can only be one iterator, so there should only be one initialization here.
- The second expression should only be concerned with the loop condition.
- The third expression should only be concerned with the iteration. Keep it as simple as possible.
Whenever the comma operator is used in a C program, it is usually an indication of bad program design (2). Such code needs to be revised in most cases.
Consider using better variable names so that the code speaks for itself, without the need for external documentation. Instead of "Cnt", I assume you mean "Count". Instead of "Drctn", I assume you mean "Direction". By "Usr" I assume you mean "User". And so on. C allows verbose variable names.
In case this is a DSP program or any other embedded system, the use of int
is dangerous and will potentially lead to bugs (3). Use the types of stdint.h instead.
To fix the code, you need to identify the loop iterator. It seems to be Cnt
. So the for loop should be for(Cnt=1; Cnt<= Rbn_Pxl; Cnt++)
.
All other variable initializations are unrelated to the loop, and should be moved out. Similarly, there are various function calls that need to be inside the actual loop; they aren't in the slightest related to loop iteration.
Note: it isn't clear whether the intention was to declare 3 variables inside the loop, or to merely assign some values to them. I'll assume declaration was the intention.
int Bw_1 = (2 - Drctn) * (Drctn % 2);
int Bw_2 = (Drctn - 3) * ((Drctn - 1) % 2);
for(int Cnt=1; Cnt<= Rbn_Pxl; Cnt++)
{
...
XFlush(Dsp);
usleep(Usr_Draw_Arcs_Delay_Usec);
}
(But please change the variable names)
Sources:
- MISRA-C:2004 13.5 and 13.6.
- MISRA-C:2004 12.10
- MISRA-C:2004 6.3