Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of "reserved for any use"?

NOTE: This is a c question, though I added c++ in case some C++ expert can provide a rationale or historical reason why C++ is using a different wording than C.


In the C standard library specification, we have this normative text, C17 7.1.3 Reserved identifiers (emphasis mine):

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

Now I keep reading answers on SO by various esteemed C experts, where they claim it is fine for a compiler or standard library to use identifiers with underscore + uppercase, or double underscore.

Doesn't "reserved for any use" mean reserved for anyone except future extensions to the C language itself? Meaning that the implementation is not allowed to use them.

While the second phrase above, regarding single leading underscore seems to be directed to the implementation?

In general, the C standard is written in a way that expects compiler vendors/library implementers to be the typical reader - not so much the application programmers.

Notably, C++ has a very different wording:

  • Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.

(See What are the rules about using an underscore in a C++ identifier?)

Is this perhaps a mix-up between C and C++ and the languages are different here?

like image 887
Lundin Avatar asked Sep 17 '18 12:09

Lundin


People also ask

Why keywords are called as reserved words?

Some use the terms "keyword" and "reserved word" interchangeably, while others distinguish usage, say by using "keyword" to mean a word that is special only in certain contexts but "reserved word" to mean a special word that cannot be used as a user-defined name.

What is a keyword or reserved word?

Keywords have a special meaning in a language, and are part of the syntax. Reserved words are words that cannot be used as identifiers (variables, functions, etc.), because they are reserved by the language. In practice most keywords are reserved words and vice versa.

Is _t reserved?

The _t suffix is not reserved by ISO 9899 as such. The future library directions for C11 revision does only say that (C11 7.31.

What is reserved words in c?

By Chaitanya Singh | Filed Under: c-programming. In C, we have 32 keywords, which have their predefined meaning and cannot be used as a variable name. These words are also known as “reserved words”. It is good practice to avoid using these keywords as variable name.


1 Answers

In the C standard, the meaning of the term "reserved" is defined by 7.1.3p2, immediately below the bullet list you are quoting:

No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

Emphasis mine: reserved identifiers place a restriction on the program, not the implementation. Thus, the common interpretation – reserved identifiers may be used by the implementation to any purpose – is correct for C.

I have not kept up with the C++ standard and no longer feel qualified to interpret it.

like image 106
zwol Avatar answered Oct 10 '22 19:10

zwol