警告:ISO C90禁止混合声明和代码[- w声明-后声明][复制]

时间:2022-07-28 16:09:10

This question already has an answer here:

这个问题已经有了答案:

I have this c file

我有这个c文件。

#include <stdio.h>

int main(int argc, char *argv[])
{
  int x,i,sum;
  sum = 0;
  FILE *fin;
  fin = fopen("testdata1", "r");


    for (i = 0; i < 20; i++ ){
      fscanf(fin, "%d", &x);
      sum += x;
    }

    printf("Sum = %d", sum);
    fclose(fin);
    return 0;


}

I compiled it via gcc -ansi -pedantic -Wall app.c -o app

我是通过gcc -ansi -pedantic -Wall app.c -o程序编译的。

While compiling, I kept getting this warning error

在编译时,我一直得到这个警告错误。

warning: ISO C90 forbids mixing declarations and code [-Wdeclaration-after-statement] FILE *fin; ^ 1 warning generated.

警告:ISO C90禁止混合声明和代码[- w声明-后声明]文件*fin;^ 1生成警告。

Any hints on how do I stop that ?

关于如何停止的任何提示?

1 个解决方案

#1


4  

This is because in C89/C90, you have to first declare (eventually initialize) your variables, then put your code. Here is the highlighted problem:

这是因为在C89/C90中,您必须首先声明(最终初始化)您的变量,然后放入您的代码。这里有一个突出的问题:

int x,i,sum;
sum = 0; // This is code!
FILE *fin;

First solution is to initialize sum in the declaration:

第一个解决方案是在声明中初始化sum:

int x,i,sum = 0;

Second solution is to initialize sum in the beginning of the code:

第二个解决方案是在代码开始时初始化sum:

int x,i,sum;
FILE *fin;

sum = 0;
fin = fopen("testdata1", "r");

Third solution is to compile using another standard. With gcc/mingw, this is achieved by passing the command-line option -std=<your standard>, for example, -std=c99 or -std=c11 (and remove -ansi).

第三个解决方案是使用另一个标准来编译。通过使用gcc/mingw,可以通过传递命令行选项-std= <您的标准> ,例如-std=c99或-std=c11(并删除-ansi)来实现。

#1


4  

This is because in C89/C90, you have to first declare (eventually initialize) your variables, then put your code. Here is the highlighted problem:

这是因为在C89/C90中,您必须首先声明(最终初始化)您的变量,然后放入您的代码。这里有一个突出的问题:

int x,i,sum;
sum = 0; // This is code!
FILE *fin;

First solution is to initialize sum in the declaration:

第一个解决方案是在声明中初始化sum:

int x,i,sum = 0;

Second solution is to initialize sum in the beginning of the code:

第二个解决方案是在代码开始时初始化sum:

int x,i,sum;
FILE *fin;

sum = 0;
fin = fopen("testdata1", "r");

Third solution is to compile using another standard. With gcc/mingw, this is achieved by passing the command-line option -std=<your standard>, for example, -std=c99 or -std=c11 (and remove -ansi).

第三个解决方案是使用另一个标准来编译。通过使用gcc/mingw,可以通过传递命令行选项-std= <您的标准> ,例如-std=c99或-std=c11(并删除-ansi)来实现。