How to declare variables in for in c?

Asked

Viewed 138 times

-1

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

  • 1

    Ideally, you put your code to the question to see what’s wrong.

  • 1

    There’s no code, only he’s wrong.

  • 4

    But it gives error where there is no code? rs..

  • I believe that because you are using version C99, you had already set up codeblocks once and it worked.

  • 4

    I don’t know which is worse, if it’s the unambiguous question, or the answers that explain how to do the exact opposite of the question. @user41625 would be nice at least [Edit] and for an example of how it worked before, so that people respond to what needs to be done to keep working. Or you can test and answer yourself, since by the C99 comment, you killed the riddle. Remember that a well-formulated question attracts more objective answers, and helps more people with the same question.

  • 2

    The worst is this comment: Não tem código, só que ele da erro. @Bacchus

  • 3

    @Bacco the worst is to have 3 positive votes in anything. mainly because it is rare a good question of C having 3 votes.

  • Sorry, I managed to solve.

  • 1

    @user41625 even so, it would be cool if you put the code you used and it didn’t work in the question, and post an answer showing how it solved, it could help other people. Note that when we talk about problems in the question, it is not to criticize you, but to point out how you can improve your post.

  • Use this macro that will definitely work #define FOR(x,y) int x; for(x=0;x<y;x++) the way to use: FOR(i,5){ puts("string"); }. Note: since the macro already makes the variable declaration, it is not possible to use the same variable within the same method, so don’t get used to using this macro.

Show 5 more comments

3 answers

1

In the C89 language, it is mandatory to declare variables at the beginning of a code block. Always after an open "{". Whether inside an if or at the beginning of the method.

It may be that before you could because the compiler you used did not use the same rules as your current compiler (it has compilers that do not only follow the C89 rules and accept extensions like GNU. With this, it is possible to declare variables out of order).

Now he won’t be anymore since it stopped working. Or you passed on to him the -pedantic flag, which instructed 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 for or return the code to be the first operation.

int i = 0;

...

for( ; i < SIZE; i++ )
{
}

1

Declares before FOR, and no is calls the declared variable

1

although the question is not very detailed you can do so:

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

or so if the variable is within the loop:

int i;
for(i = 0; i < 10; i++){
   int j = 2;
   printf("%d\n", i*j);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.