所有这些真的都是 C 中的关键字吗?

Are all of these really keywords in C?

我是计算机科学专业的学生,​​我们的教授最近举办了一场关于 C 基础知识的演讲。它的脚本声明 "key words are words with a fixed meaning, that are always written in lower case."

首先,我知道有_Bool这样的关键字,所以这个说法已经是有问题的了。该脚本还包含一堆示例,包括但不限于:

这引出了很多问题:

此外,如果我想证明关于 C 的陈述,我在哪里可以找到官方来源来引用?

关键字是语言保留的合法标识符。

运算符不是关键字。 #include 不是关键字。 main不是关键字(其实可以用main作为变量名)。

您将引用 C 规范:

6.4.1 Keywords

Syntax

keyword: one of

auto break case char const continue default do double else enum extern float for goto if inline int long register restrict return short signed sizeof static struct switch typedef union unsigned void volatile while _Bool _Complex _Imaginary

Semantics

The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise. The keyword _Imaginary is reserved for specifying imaginary types.

来自 ISO/IEC 9899:TC3 参见 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

有关标准文档的链接,请参阅 these two 答案。您通常可以安全地使用免费的后期草稿作为参考,而不是相当昂贵的最终文件。至少对于 C++ 而言,两者之间的差异(主要)是次要的编辑细节。

C

C11 标准在第 6.4.1 节中定义了以下关键字:

auto        if          unsigned
break       inline      void
case        int         volatile
char        long        while
const       register    _Alignas
continue    restrict    _Alignof
default     return      _Atomic
do          short       _Bool
double      signed      _Complex
else        sizeof      _Generic
enum        static      _Imaginary
extern      struct      _Noreturn
float       switch      _Static_assert
for         typedef     _Thread_local
goto        union

没有方便的单个运算符列表,但它们没有作为关键字列出。任何预处理器指令也不是。

C++

C++14 标准在第 2.12 节中定义了以下关键字:

alignas     continue        friend      register          true
alignof     decltype        goto        reinterpret_cast  try
asm         default         if          return            typedef
auto        delete          inline      short             typeid
bool        do              int         signed            typename
break       double          long        sizeof            union
case        dynamic_cast    mutable     static            unsigned
catch       else            namespace   static_assert     using
char        enum            new         static_cast       virtual
char16_t    explicit        noexcept    struct            void
char32_t    export          nullptr     switch            volatile
class       extern          operator    template          wchar_t
const       false           private     this              while
constexpr   float           protected   thread_local
const_cast  for             public      throw

一些运算符的替代符号也被视为关键字:

and     and_eq  bitand  bitor   compl   not
not_eq  or      or_eq   xor     xor_eq

操作符在2.13节中列出,但不叫关键字。就像在 C 预处理器中一样,符号也不是关键字。

您的教授名单

严格按照你教授的标准写错了。您问题中的列表片段列出的不仅仅是关键字。它看起来更像是具有预定义含义的特殊单词和符号的列表,作为语言或预处理器中的标记。

当您查看两个标准中关键字的半显式半定义时,这种解释实际上 并不牵强:

C11

The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise.

C++14

The identifiers shown in Table 4 are reserved for use as keywords (that is, they are unconditionally treated as keywords in phase 7) except in an attribute-token.