[英]CS50 Assignment Issue; expected identifier or '('
我正在将此示例代码复制并粘贴到 CS50 IDE 中,
文件名 test.c:
#include <cs50.h>
#include <stdio.h>
for (int i = 0; i < 10, i++)
{
printf("test this");
}
尝试编译时出现以下错误:
~/workspace/pset1/ $ make test
clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow test.c -lcrypt -lcs50 -lm -o test
test.c:4:1: error: expected identifier or '('
for (int i = 0; i < 10, i++)
^
1 error generated.
make: *** [test] Error 1
我查看了所有我发现的答案都包含语法错误,但是在碰到这堵墙后我一直在复制/粘贴示例代码。
如果那是您的整个文件,我想您会发现代码必须包含在某个描述的函数中,可能是main
:
#include <stdio.h>
int main (void) {
for (int i = 0; i < 10; i++) {
printf ("test this\n");
}
return 0;
}
您会看到我所做的其他一些更改,这些更改应该会使其“更好”:
for
循环。谢谢 - 我需要将循环封装在一个函数中,并且循环本身存在语法错误(逗号而不是分号)。
如前所述,C 语法要求在 for 循环中使用分号。 您的 for 应该是:
for (int i = 0; i < 10; i++)
在像这样的 for 循环中,您需要使用分号而不是逗号。 我必须是这样的:
#include <cs50.h>
#include <stdio.h>
for (int i = 0; i < 10; i++)
{
printf("test this");
}
不是这个:
#include <cs50.h>
#include <stdio.h>
for (int i = 0; i < 10, i++)
{
printf("test this");
}
如您所见,您必须在后面使用分号而不是逗号
i < 10
部分。
您使用了逗号,您需要在循环中使用分号,因为 c 要求您这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.