我的代码有什么问题?
#include<stdio.h>
#include<string.h>
int main() {
FILE *file;
char string[32] = "Teste de solução";
file = fopen("C:\file.txt", "w");
printf("Digite um texto para gravar no arquivo: ");
for(int i = 0; i < 32; i++) {
putc(string[i], file);
}
fclose(file);
return 0;
}
错误:
c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2143: syntax error : missing ';' before 'type'
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2143: syntax error : missing ';' before 'type'
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2143: syntax error : missing ')' before 'type'
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2143: syntax error : missing ';' before 'type'
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2065: 'i' : undeclared identifier
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): warning C4552: '<' : operator has no effect; expected operator with side-effect
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2065: 'i' : undeclared identifier
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2059: syntax error : ')'
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(13): error C2143: syntax error : missing ';' before '{'
1>c:\users\guilherme\documents\visual studio 2010\projects\helloworld\helloworld\hello.c(14): error C2065: 'i' : undeclared identifier
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
最佳答案
显然,您将其编译为C,而不是C ++。 VS不支持C99,在这种情况下,您可能不会这样做:
for (int i = 0; i < 32; i++)
您需要执行以下操作:
int i;
...
for(i = 0; i < 32; i++)
i
的声明必须在函数中所有语句之前出现。关于c - Visual Studio Express提示缺少';'键入c程序后,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15260890/