C - Abbreviated for loop executed only once

c, for-loop

Solution

If you leave out the `b=0` the inner loop will run exactly once, because after that b is already equal to size. You need to reset b to 0 on each iteration of the inner loop.

Problem

I have command line utility written in ANSI C on a Mac with a function to create a bubble sorted array for a single-linked list. I declared the loop variables. ``` int a = 0; int b = 0; ``` I wrote the bubble sort for loops in the abbreviated style (i.e., leaving the variable initialization empty). ``` for ( ; a < size; a++) for( ; b < size; b++) ``` This executed only once before exiting. A previous for loop using the i variable to populate the array was written the same way and executed as expected. The fix for the bubble sort loops was to put a = 0 and b = 0 back in. Is there a reason why the abbreviated for loops failed to execute?

Original source