• 错误:在 C99 模式之外使用‘for’循环初始化声明


    slrps@linux-vnbn:~/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就解决了。

  • 相关阅读:
    How to configure security of ActiveMQ ?
    CentOS 搭建 nginx + tomcat
    25个 Git 进阶技巧
    写给Git初学者的7个建议
    my links
    Shell scripts to Create a local dir base on the time.
    81For全栈技术网
    一款可视化的在线制作H5
    在线制作h5
    在线制作h5——上帝的礼物
  • 原文地址:https://www.cnblogs.com/zhanglibra/p/4495100.html
Copyright © 2020-2023  润新知