比较两个程序: 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\n", s);
    printf("i = %d\n", 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\n", s);
    printf("s = %d\n", i);
    return 0;
}

 

哪一个会正常运行呢?

答案是第二个;

第一个会出现如下错误:

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM