C语言 for循环中的“预期标识符或”(“)[已关闭]

quhf5bfb  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(99)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受回答。

这个问题是由错字或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
16天前关闭
Improve this question
我正在编译这段代码

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int n = get_int("How many powers of 2 do you want? ");

    int power[n];
    power[0] = 1;

    for (int i = 1, int j = 0; i < n; i++, j++)
    {
        printf("%i", power[j]);
        power[i] = power[i - 1] * 2;
    }
}

字符串
但是这个错误出现了:

pw.c:11:21: error: expected identifier or '('
    for (int i = 1, int j = 0; i < n; i++, j++)
                    ^


我想问题是因为我试图在for循环中定义两个变量,但我不知道为什么。

ct2axkht

ct2axkht1#

C中逗号分隔的声明都是相同的类型,并且只有一个类型说明符:

for (int i = 1, j = 0; i < n; i++, j++)

字符串
事实上,它在for语句中并没有什么区别-如果你将它们声明为独立的:

int i = 1, int j = 0 ;


由于同样的原因无效,它应该是:

int i = 1, j = 0 ;


要有单独的类型,它们需要是单独的语句:

int i = 1; int j = 0 ;


这当然在for语句中不起作用。

相关问题