What is the difference between “keyword” and “reserved word”?

前端 未结 10 2472
再見小時候
再見小時候 2020-12-13 05:54

What\'s the difference between a keyword and a reserved word?

For example, in the proposal for concepts in C++ one can read the following statement

10条回答
  •  天命终不由人
    2020-12-13 06:31

    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. But because they're two different things it may happen that a keyword is not a reserved word (e.g. a keyword only has meaning in a special context, and can therefore be used as an identifier), or a reserved word is not a keyword (e.g. because it is reserved for future use).

    Update: Some examples as given by others that illustrate the distinction:

    • In Java, goto is a reserved word but not a keyword (as a consequence, you cannot use it at all)
    • Fortran has no reserved words, all keywords (if, then, etc.) can be used as identifiers

提交回复
热议问题