比较两个程序: error: ‘i’ undeclared (first use in this function)

程序一:

#include <stdio.h>
int main()
{
    int s = 0;
    for (int i = 1; i <= 10; i+=3) {
        s += i;
    }
    printf("s = %d
", s);
    printf("i = %d
", i);
    return 0;
}

程序二:

#include <stdio.h>
int main()
{
    int s = 0;
    int i;
    for (i = 1; i <= 10; i+=3) {
        s += i;
    }
    printf("s = %d
", s);
    printf("s = %d
", i);
    return 0;
}

哪一个会正常运行呢?

答案是第二个;

第一个会出现如下错误:

error: ‘i’ undeclared (first use in this function)
printf("i = %d ", i);

原文地址:https://www.cnblogs.com/profesor/p/12750341.html