[email protected]:~/BeginningC/chapter12> gcc Program12.1.c -o Program12.1
Program12.1.c: 在函数‘main’中:
Program12.1.c:24: 错误:在 C99 模式之外使用‘for’循环初始化声明
#include <stdio.h> #include <stdlib.h> #include <string.h> const int LENGTH = 80; int main(void) { char mystr[LENGTH]; int lstr = 0; int mychar = 0; FILE *pfile = NULL; char *filename = "myfile.txt"; printf("n\Enter an intersting string of less than 80 characters:\n"); fgets(mystr, LENGTH, stdin); if (!(pfile = fopen(filename, "w"))) { printf("Error opening %s for writing.", filename); exit(1); } lstr = strlen(mystr); for (int i = lstr - 1; i >= 0; i--) { fputc(mystr[i], pfile); } fclose(pfile); if (!(pfile = fopen(filename, "r"))) { printf("Error opening %s for reading.", filename); exit(2); } while ((mychar = fgetc(pfile)) != EOF) putchar(mychar); putchar(‘\n‘); fclose(pfile); return 0; }
错误处理:
C99 允许在for语句的 “表达式1 ”中定义并初始变量, gcc4 编译c语言的默认标准是C89, 编译C99程序需加参数 “–-std=c99” ;使用
gcc Program12.1.c -o Program12.1 --std=c99就解决了。
时间: 2024-11-01 21:36:09