简体   繁体   English

ISO C90 禁止在 C 中混合声明和代码

[英]ISO C90 forbids mixed declarations and code in C

I declared a variable in this way:我以这种方式声明了一个变量:

int i = 0;

I get the warning:我收到警告:

ISO C90 forbids mixed declarations and code ISO C90 禁止混合声明和代码

How can I fix it?我该如何解决?

I think you should move the variable declaration to top of block.我认为您应该将变量声明移动到块的顶部。 Ie IE

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

to

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

Up until the C99 standard, all declarations had to come before any statements in a block:在 C99 标准之前,所有声明都必须出现在块中的任何语句之前:

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++). C99 允许混合声明和语句(如 C++)。 Many compilers still default to C89, and some compilers (such as Microsoft's) don't support C99 at all .许多编译仍然默认为C89,和一些编译器(如微软的)根本就不支持C99。

So, you will need to do the following:因此,您需要执行以下操作:

  1. Determine if your compiler supports C99 or later;确定您的编译器是否支持 C99 或更高版本; if it does, configure it so that it's compiling C99 instead of C89;如果是,请对其进行配置,使其编译 C99 而不是 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.如果你的编译器不支持C99或更高版本,你要么需要找到一个不同的编译器支持它,或者重写代码,使所有声明的块内的所有语句之前。

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.只需使用编译器(或为其提供所需的参数),以便为更新版本的 C 标准、C99 或 C11 进行编译。 Eg for the GCC family of compilers that would be -std=c99 .例如,对于 GCC 系列的编译器,将是-std=c99

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:确保变量位于块的顶部,如果您使用-ansi-pedantic编译它,请确保它看起来像这样:

function() {
    int i;
    i = 0;

    someCode();
}

To diagnose what really triggers the error, I would first try to remove = 0要诊断真正触发错误的原因,我会首先尝试删除= 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.如果没有错误,那么它可能与 C 标准强制/编译标志或...其他东西有关。

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.如果 OP 可以澄清上下文,那么将会有更直接的回应。

-Wdeclaration-after-statement minimal reproducible example -Wdeclaration-after-statement最小可重现示例

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.在 Ubuntu 16.04、GCC 6.4.0 上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM