2

I am computer science student and our professor recently held a presentation on C basics. Its script states that "key words are words with a fixed meaning, that are always written in lower case."

First of all, I know that there is keywords such as _Bool, thus this statement is already flawed. The script also contains a bunch of examples that include, but are not limited to:

  • break
  • case
  • void
  • main
  • &
  • {
  • #include
  • %

This begs many questions:

  • can operators be classified as keywords?
  • are compiler instructions such as #include keywords?
  • can main be seen as such special of a function name that it is classifiable as a keyword?

And also, where do I find the official sources to quote if I ever wanted to prove a statement about C?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96

2 Answers2

3

See these two answers for links to the standards documents. You can usually safely use the free late-draft versions as a reference instead of the rather expensive final documents. At least for C++ the differences between the two are (mostly) minor editorial details.

C

The C11 standard defines the following keywords in section 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

There is no convenient single list of operators, but they are not listed as keywords. Neither are any preprocessor directives.

C++

The C++14 standard defines the following keywords in section 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

The alternative notations for some operators are are considered keywords as well:

and     and_eq  bitand  bitor   compl   not
not_eq  or      or_eq   xor     xor_eq

The operator symbols are listed in section 2.13, but they are not called keywords. And like in C preprocessor symbols are not keywords either.

Your Professor’s list

Strictly according to the letter of the standards your professor is wrong. The snippet of the list in your question lists more than keywords. It looks more like a list of special words and symbols with a pre-defined meaning as tokens in the language or preprocessor.

That interpretation is not actually that far-fetched when you look at the half-explicit semi-definitions of what a keyword is in the two standards:

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.

besc
  • 2,507
  • 13
  • 10
2

Keywords are otherwise-legal identifiers that are reserved by the language.

Operators are not keywords. #include is not a keyword. main is not a keyword (in fact, you can use main as a variable name).

You would quote the C spec:

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.

From ISO/IEC 9899:TC3 See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

Jacob Krall
  • 28,341
  • 6
  • 66
  • 76
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • if you define as "otherwise-legal identifiers that are reserved by the language" then they include much more: basically all words "starts with an underscore followed by another underscore or upper case letter" becomes keywords. – user3528438 Oct 14 '16 at 18:30
  • The standards describe reserved identifiers and keywords differently. – Peter Oct 15 '16 at 08:20