What happens if you put ';' after for(...)?

Look at this code

1
2
3
4
	  for (a=0; a<10; a++)
	  {
		  printf ("%d",matrix1[a]);
	  }


1
2
3
4
	  for (a=0; a<10; a++);
	  {
		  printf ("%d",matrix1[a]);
	  }


For the 1st code i get 10 zeros which are the matrix contents.

For the 2ond code I dont get an error but a "4006960"

In what is the for function transformed if you put a ";" in the end?

--------------------------------------------------------------------------------------------
My blog [http://georgesblogprogramming.blogspot.com]. Personal notes and experiences in the learning of C
Last edited on
In the first case, it becomes an empty loop. The same applies for the second, but the block below it is executed just once.

In the first case, the block below the for gets executed 10 times. In the second case, the semicolon makes the loop empty. So it runs ten times without doing nothing, and then executes the code below.

And for is not a function.
Last edited on
@helios
Isnt the first one supposed to run properly, since it has no semi-colon and all?

The second loop should increment a ten times and stop, since there are no statements in it. It should then execute the code in the block. Once.
Last edited on
Oh, I didn't notice. That's weird. Fixed.
Topic archived. No new replies allowed.