首页 > 解决方案 > 如何解决这个语法错误?(使用 C)

问题描述

我开始学习 C 编程语言并写了流行的“Hello world!” 程序。但是当我尝试执行它时,会出现

Traceback (most recent call last):
  File "C:\Users\asebo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\asebo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\asebo\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\asebo\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
    run()
  File "c:\Users\asebo\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "C:\Users\asebo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 267, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "C:\Users\asebo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 242, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "c:\Users\asebo\OneDrive\Počítač\codeblock\c.c", line 2
    int main() {
        ^
SyntaxError: invalid syntax

这是我的完整代码:

#include <stdio.h>
int main() {
   printf("Hello, World!");
   return 0;
}

PS:我正在使用 VS 代码 IDE

标签: cvisual-studio-codesyntax-error

解决方案


看起来你的编译输出中有一些 Python 路径,为什么?

如果我是你,我只会创建一个 C++ 控制台应用程序。在VS中,编写它并保存为C代码,给它一个文件名,即“fileName.c”并从VS文件菜单调试它。


推荐阅读