ISO C90 prohibits mixed declarations and code in C

I declared the variable like this:

int i = 0; 

I get a warning:

ISO C90 prohibits mixed declarations and code

How can i fix this?

+45
c variables
Nov 08
source share
5 answers

I think you should move the variable declaration to the top of the block. I.e.

 { foo(); int i = 0; bar(); } 

to

 { int i = 0; foo(); bar(); } 
+76
Nov 08
source share

Prior to the C99 standard, all announcements should have appeared before any statements in the block:

 void foo() { int i, j; double k; char *c; // code if (c) { int m, n; // more code } // etc. } 

C99 is allowed to mix declarations and statements (e.g. C ++). Many compilers still use C89, and some compilers (like Microsoft) do not support C99 at all.

So, you will need to do the following:

  • Determine if your compiler supports C99 or later; if so, configure it so that it compiles C99 instead of C89;

  • If your compiler does not support C99 or later, you need to either find another compiler that supports it, or rewrite the code so that all declarations come before any statements inside the block.

+17
Nov 08 '12 at 16:22
source share

Just use the compiler (or specify it with the necessary arguments) so that it compiles for a newer version of the standard C, C99 or C11. For example, for GCC family compilers that would be -std=c99 .

+10
Nov 08
source share

Make sure the variable is at the top of the block, and if you compile it with -ansi-pedantic , make sure it looks like this:

 function() { int i; i = 0; someCode(); } 
+3
May 9 '13 at 9:05
source share

To diagnose what really causes the error, I would first try to remove = 0

  • If the error worked, then most likely the ad will follow the code.

  • If there is no error, then this may be due to the C-standard OR enforcement / compilation flags ... something else.

In either case, declare a variable at the beginning of the current scope. Then you can initialize it separately. Indeed, if this variable deserves its own scope, divide its definition by {}.

If the OP can clarify the context, then a more directed answer will follow.

+2
Nov 08
source share



All Articles