此循环输出始终相同的数字(iPad c编译器)

时间:2016-10-11 06:24:59

标签: c for-loop output

#include <stdio.h>
#include <math.h>

int main(void) 
{
    printf("Hello, World!\n");
    int n;

    for(int i = 1; i<30; i++) 
    {
        n = i*i+i+i+i+1;
        printf("\n");
    }

    return 0;
}

我希望此代码为i的每个值执行一定数量的代码。但是它始终输出664。 谁知道为什么?

2 个答案:

答案 0 :(得分:4)

如果我理解正确的话,我想你忘了打印n

命令printf("\n");应该只打印一个新行。

请改为:printf("%d\n",n);

答案 1 :(得分:0)

添加

   printf("%d\n",n); 

使用你的代码我得到编译错误&#34;错误:'for'循环初始声明在C99模式之外使用&#34;

你打开C99模式编译了吗?

使用

    -std=c99 

成功编译后(注意:添加上面的printf行)得到了不同的执行值。