gpt4 book ai didi

c - 终端未运行 C

转载 作者:行者123 更新时间:2023-11-30 20:23:56 25 4
gpt4 key购买 nike

所以我尝试运行一个简单的代码进行试用,看看我如何在终端中运行 C 代码,但出现此错误:

main.c: line 3: syntax error near unexpected token `('
main.c: line 3: `int main(int argc, const char * argv[]) {'

有什么想法为什么会发生这种情况吗?在 xcode 上运行正常吗?

我运行的代码:

#include <stdio.h>

int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
for (int i = 0; i <= 10;i++)
{
printf("IT WORKS!\n");
}
return 0;
}

运行文件的命令:

bash main.c

最佳答案

此错误是从尝试将您的 c 文件解析为 shell 脚本的 shell 返回的,因为您尝试直接运行 C 代码。

sh-3.2$ cat > mycode.c
#include <stdio.h>

int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
for (int i = 0; i <= 10;i++)
{
printf("IT WORKS!\n");
}
return 0;
}
sh-3.2$ chmod +x mycode.c
sh-3.2$ ./mycode.c
./mycode.c: line 3: syntax error near unexpected token `('
./mycode.c: line 3: `int main(int argc, const char * argv[]) {'

不能直接运行C代码,首先需要将其编译成可执行文件,然后执行编译后的可执行文件。

例如:

$ gcc -o myexe main.c
$ ./myexe

关于c - 终端未运行 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35021776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com