简体   繁体   English

(第一个 C 程序 / Hello World)不断收到语法错误

[英](First C Program / Hello World) Keep getting Syntax Error

This is my current code:这是我当前的代码:

#include <stdio.h>
    
void print_hello() {
    printf("Hello n10321234, welcome to BSB211");
}
    
int main() {
    print_hello();
    return 0;
}

However I keep getting the following errors when I compile and run the .exe:但是,当我编译和运行 .exe 时,我不断收到以下错误:

./print_hello: line 3: syntax error near unexpected token (' ./print_hello: 第 3 行:意外标记附近的语法错误 ('

./print_hello: line 3: `void print_hello(){' ./print_hello: 第 3 行:`void print_hello(){'

For compiling I used gcc print_hello.c -o print_hello and to run I use ./print_hello为了编译我使用gcc print_hello.c -o print_hello并运行我使用./print_hello

Your code is correct.你的代码是正确的。 I compiled and run it successfully.我编译并运行成功。

What happened is you've accidentally copied print_hello.c to print_hello.发生的事情是您不小心将print_hello.c 复制到了print_hello。 You're running this file as a script and get a script error.您将此文件作为脚本运行并收到脚本错误。 please make sure you compile correctly before running print_hello.请确保在运行 print_hello 之前正确编译。

You can try to delete print_hello then run your gcc again.您可以尝试删除 print_hello 然后再次运行您的 gcc。

This is what happens when I try to run the code as a script(exactly the same error)当我尝试将代码作为脚本运行时会发生这种情况(完全相同的错误)

>./hello.c
./hello.c: line 3: syntax error near unexpected token `('
./hello.c: line 3: `void print_hello() {'

Your code seems to be correct.您的代码似乎是正确的。 I ran it on my machine and works fine.我在我的机器上运行它并且工作正常。

I also ran it on ideone and it works fine.我也在 ideone 上运行它,它工作正常。

Try reinstalling/updating your gcc.尝试重新安装/更新您的 gcc。 Maybe that will help.也许这会有所帮助。

Alternatively, you can create an Ubuntu Virtual Machine and try it there.或者,您可以创建一个 Ubuntu 虚拟机并在那里试用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM