gpt4 book ai didi

c - C 脚本中的意外标记 "("

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:16 25 4
gpt4 key购买 nike

我正在尝试编写一个创建子进程的 C 脚本,并根据返回值打印一条消息。到目前为止我写了这段代码:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>

int main ()
{
pid_t child_pid;
printf ("the main program process ID is %d\n", (int) getpid());
child_pid = fork() ;
if (child_pid==0){
printf("this is the child process,with the id %d\n",(int) child_pid );
}
else
printf("this is the parent process with id %d\n",(int) getpid ());

return 0;
}

当我尝试编译它时,它似乎没问题。但是当我在终端中运行它时,出现以下错误:

./lab7.c: line 6: syntax error near unexpected token `('
./lab7.c: line 6: `int main ()'"

有人可以告诉我我做错了什么吗?

最佳答案

听起来您正在将源代码作为脚本而不是可执行文件来执行,而且,毫不奇怪,无论您在哪个 shell 中都不喜欢它。

您需要先编译代码,然后执行编译结果。例如,这里我们在第一行编译生成名为 lab7 的可执行文件,并在第二行执行它:

gcc -Wall -Wextra lab7.c -o lab7
./lab7

关于c - C 脚本中的意外标记 "(",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29447904/

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