How to declare variables in for for in c? [closed]

-1

It's the error, when I try to declare a variable in a loop as for, while. Before I could, does it have to do with the language version?

    
asked by anonymous 01.03.2016 / 20:46

3 answers

1

Declare before the FOR, and in the calls the declared variable

    
01.03.2016 / 20:50
1

Although the question is not very detailed you can do this:

for(int i = 0; i < 10; i++)
  /* seu código aqui*/

or this way if the variable is inside the loop:

int i;
for(i = 0; i < 10; i++){
   int j = 2;
   printf("%d\n", i*j);
}
    
01.03.2016 / 21:15
1

In language C89, the declaration of variables at the beginning of a code block is mandatory. Always after one opens "{". Be it within an if or at the beginning of the method.

It might be that before you did because the compiler you used did not use the same rules as your current compiler (there are compilers that do not just follow C89 rules and accept extensions like GNU. out of order).

Now it should not be anymore as it has stopped working. Or you gave him the flag -pedantic, which instructs the compiler to follow the pattern to the letter.

To avoid this problem, remove the flag, change the compiler, or declare int i at the beginning of the block and use the for or return code for it to be the first operation.

int i = 0;

...

for( ; i < SIZE; i++ )
{
}
    
01.03.2016 / 20:48