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

This is a discussion on What happens if you put ';' after for(...)? within the C Programming forums, part of the General Programming Boards category; Look at this code Code: for (a=0; a<10; a++) { printf ("%d",matrix1[a]); } Code: for (a=0; a<10; a++); { printf ...

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

Look at this code

Code:
```	  for (a=0; a<10; a++)
{
printf ("%d",matrix1[a]);
}```
Code:
```	  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?

2. If you do that the loop body will be an empty statement (made up of no code). a will be incremented till it is 10 and then you'll access matrix1[10] which might be out of bounds.

It is the equivalent of
Code:
```    for (a=0; a<10; a++)
{
; //or nothing here
}
printf ("%d",matrix1[a]);```

3. This:
Code:
```for (a=0; a<10; a++);
{
printf("&#37;d", matrix1[a]);
}```
Is equivalent to:
Code:
```for (a=0; a<10; a++)
{
/* empty statement */
}

{
printf("%d", matrix1[a]);
}```
You get a random value printed because a is initialised to garbage, and thus matrix1[a] likely accesses the array out of bounds, so you got some garbage at the given location.

EDIT:
Oops, yes, a is set to 10, not initialised to garbage... but then that matrix1[10] is out of bounds applies.

4. yeah I understand, since the last cell of matrix[10] is 9 not 10

Thanks

P.S.
These questions might be silly for troubling an entire forum but unlike C++, this time in C I dont want to leave holes of knowledge. When many of them gatehred, then when you're creating complex programms you get errors like this and you say "why"

My Blog. Personal Notes and experiences in the learning of C

5. Eh guys this crashes! The programm runs but when I insert a value it says programm has to shut down!
Code:
```      int a=0;
int i;
float b=3;
int matrix1 [10]={0};
for (i=0; i<10; i++)
{
printf ("&#37;d",matrix1[i]);
}
for (i=0; i<10; i++)
{
printf ("\nGive value for cell %d\n",i);
scanf ("%d",matrix1[i]);
}
for (i=0; i<10; i++)
{
printf ("Cell %d",i,"is%d",matrix1[i]);
}```

6. Well yeah, you're not passing the address of matrix1[i] to scanf, but instead its value.

7. Of course! I have to put just the matrix name!

P.S.
This complex form of printf is illegal?
Code:
`printf ("Cell &#37;d",i,"is%d",matrix1[i]);`

8. Yes, it is.

9. Unfortunately it is a very common mistake to put ; after for (by accident). It took me once several hours to find out what was the problem :/

10. You realize there are debuggers, right? Should be very easy to spot with them.

11. My bloodshed i have a feeling that doesnt shows warnings, just errors.

12. Originally Posted by George M.
Of course! I have to put just the matrix name!

P.S.
This complex form of printf is illegal?
Code:
`printf ("Cell %d",i,"is%d",matrix1[i]);`
Actually, that is perfectly legal printf statement - the only problem is that the format string is not matching the arguments, as you have one integer format, and you provide one integer, a string and another integer argument.

Obviously, it also doesn't do what I think you expect it to do. It would print only the first integer argument. To print multiple values, you'd make the first string contain all the formats, and then put all the arguments after that. That means, move the "i" to after the format string, and merge the two format portions to one string.

--
Mats

13. Yes it shows only the first number

I want the i to appear between those 2 texts not both i and matrix toghether

But you confused me...

Would be easier for me if you wrote the correct expression. Easier for you too I think

14. printf ("Cell &#37;d is %d",i, matrix1[i]);

15. oh......... so it appears where th "&#37;" ares (how are they called?)

So thats no a symbol to decalarate end of text but to declarate placing of a variable there wright?

Page 1 of 2 12 Last