104

I declared a variable in this way:

int i = 0;

I get the warning:

ISO C90 forbids mixed declarations and code

How can I fix it?

7
  • 3
    It sounds like GCC's -pedantic or -std=c89, in which case you can compile with -std=gnu99 if you like. Nov 8, 2012 at 14:56
  • 3
    If you are using gcc you can specify to use C99 which allows mixed declarations and code (flag -std=c99).
    – hmjd
    Nov 8, 2012 at 14:56
  • 13
    One option would be to stop compiling to a standard that's 22 years old for which even the replacement has been replaced. Nov 8, 2012 at 14:56
  • 6
    @StephenCanon, please tell Microsoft that. :)
    – hmjd
    Nov 8, 2012 at 14:56
  • 2
    @hmjd: Microsoft has no interest in shipping a C compiler; that will likely not change. Fortunately, there are a number of perfectly good compilers targeting the platform supplied by other vendors. Nov 8, 2012 at 14:57

7 Answers 7

160

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

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

to

{
    int i = 0;
    foo();
    bar();
}
4
  • 5
    ... or open a fresh context: {foo(); {int i=0; bar();}}
    – alk
    Nov 8, 2012 at 14:58
  • 3
    @alk s/context/block That's what it is called in the C Standard.
    – Jens
    Jun 8, 2013 at 19:12
  • 1
    @Johan Kotlinski But why does this matter?
    – ocean800
    Feb 14, 2016 at 23:15
  • 3
    @ocean800 The C90 specification said that you cannot have declarations after non-declarations in the same block.
    – M.M
    Feb 16, 2017 at 1:40
46

Up until the C99 standard, all declarations had to come before any statements in a block:

void foo()
{
  int i, j;
  double k;
  char *c;

  // code

  if (c)
  {
    int m, n;

    // more code
  }
  // etc.
}

C99 allowed for mixing declarations and statements (like C++). Many compilers still default to C89, and some compilers (such as Microsoft's) don't support C99 at all.

So, you will need to do the following:

  1. Determine if your compiler supports C99 or later; if it does, configure it so that it's compiling C99 instead of C89;

  2. If your compiler doesn't support C99 or later, you will either need to find a different compiler that does support it, or rewrite your code so that all declarations come before any statements within the block.

11

Just use a compiler (or provide it with the arguments it needs) such that it compiles for a more recent version of the C standard, C99 or C11. E.g for the GCC family of compilers that would be -std=c99.

2
  • 6
    This answer is incomplete at best. It does fix the problem but doesn't explain what caused it. This answer excludes situations where a newer compiler isn't possible (several possible reasons ...) or build environments where switching options to the compiler like this isn't possible. A down vote because I can't agree this is the "real" answer. Jul 8, 2015 at 18:44
  • 1
    In some cases -std=gnu89 is used when compiling linux kernel modules.
    – cbix
    Mar 13, 2017 at 20:36
6

Make sure the variable is on the top part of the block, and in case you compile it with -ansi-pedantic, make sure it looks like this:

function() {
    int i;
    i = 0;

    someCode();
}
1
  • 1
    You can still initialize the variable, you just can't place code before you declare it.
    – S.S. Anne
    Jun 12, 2019 at 23:54
2

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

  • If the error is tripped, then most likely the declaration goes after the code.

  • If no error, then it may be related to a C-standard enforcement/compile flags OR ...something else.

In any case, declare the variable in the beginning of the current scope. You may then initialize it separately. Indeed, if this variable deserves its own scope - delimit its definition in {}.

If the OP could clarify the context, then a more directed response would follow.

1

-Wdeclaration-after-statement minimal reproducible example

main.c

#!/usr/bin/env bash

set -eux

cat << EOF > main.c
#include <stdio.h>

int main(void) {
    puts("hello");
    int a = 1;
    printf("%d\n", a);
    return 0;
}
EOF

Give warning:

gcc -std=c89 -Wdeclaration-after-statement -Werror main.c
gcc -std=c99 -Wdeclaration-after-statement -Werror main.c
gcc -std=c89 -pedantic -Werror main.c

Don't give warning:

gcc -std=c89 -pedantic -Wno-declaration-after-statement -Werror main.c
gcc -std=c89 -Wno-declaration-after-statement -Werror main.c
gcc -std=c99 -pedantic -Werror main.c
gcc -std=c89 -Wall -Wextra -Werror main.c
# https://stackoverflow.com/questions/14737104/what-is-the-default-c-mode-for-the-current-gcc-especially-on-ubuntu/53063656#53063656
gcc -pedantic -Werror main.c

The warning:

main.c: In function ‘main’:
main.c:5:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
     int a = 1;
     ^~~

Tested on Ubuntu 16.04, GCC 6.4.0.

1

Ensure you made your declarations before any statements in the block. For instance:

{
int i = 0;
printf("a string");/*the code is not a working code*/
}

I believe this should be helpful:

https://www.configrouter.com/iso-c90-forbids-mixed-declarations-and-code-in-c-30621/

1
  • The other answers already have this information. Oct 4, 2022 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.