>>all the variables must be declared at the top of that function
No, as said by Salem, local variables must be declared at the top of the code block, not the function.
So, a very simplistic example would be this:
Here, j is declared within the for loop, and therefore it's scope is limited to that code block.
for (i = 0; i < 10; i++)
j = i * 10;
printf ("%d * 10 is %d\n", i, j);
Is this good programming style? Well, that's another conversation that I'm sure someone will argue to death in a 50 post long thread, but imho, if it suits your needs, use it.