C语言 为什么我不能在我的 C 代码中使用 // 风格的注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2223541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:25:32  来源:igfitidea点击:

Why can't I use //-style comments in my C code?

c

提问by rlb.usa

I am using gcc(Ubuntu 4.4.1-4ubuntu9) to compile a program that I'm writing, but it seems to vomit whenever it sees a // comment in my code, saying:

我正在使用gcc(Ubuntu 4.4.1-4ubuntu9) 编译我正在编写的程序,但它似乎在我的代码中看到 // 注释时呕吐,说:

interface.c :##: error: expected expression before a/a token<

Does the gcccompile mode I'm using forbid //comments?

gcc我使用的编译模式是否禁止//注释?

$ gcc -g -ansi -pedantic interface.c structs.h -c -I. -I/home/me/project/h

Why?

为什么?

回答by fortran

//comments are not allowed in old (pre 99) C versions, use /**/(or remove the -ansi, that is a synonym for the C89 standard)

//在旧的(99 之前)C 版本中不允许使用注释,请使用/**/(或删除-ansi,这是 C89 标准的同义词)

回答by Sinan ünür

See C++ commentsin GNU compiler documentation.

请参阅GNU 编译器文档中的C++ 注释

In GNU C, you may use C++ style comments, which start with //and continue until the end of the line. Many other C implementations allow such comments, and they are included in the 1999 C standard. However, C++ style comments are not recognized if you specify an -stdoption specifying a version of ISO C before C99, or -ansi(equivalent to -std=c89).

在 GNU C 中,您可以使用 C++ 风格的注释,从行开始//一直到行尾。许多其他 C 实现允许这样的注释,它们包含在 1999 C 标准中。但是,如果您-stdC99、 或-ansi(等效于-std=c89)之前指定指定 ISO C 版本的选项,则无法识别 C++ 样式注释。

(Emphasis is mine because some of the posts claim that //are not allowed in standard C whereas that is only true for pre-99 standards).

(重点是我的,因为有些帖子声称//在标准 C 中是不允许的,而这仅适用于 99 之前的标准)。