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

前端 未结 10 2433
再見小時候
再見小時候 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:20

    I guess keyword is a word used as "keyword" (like if, for, switch, etc...) while a reserved word is something you cannot use as variable name because it might become a keyword in a future version of the language.

    0 讨论(0)
  • 2020-12-13 06:21
    • Keyword: It has some meaning and we can use in program.
    • Reserved word: We can't use in program. They may be used in future. Example: goto
    0 讨论(0)
  • 2020-12-13 06:25

    Reserved words and keywords are mostly the same and they have pre-defined meanings in GW-BASIC...these have pre-defined uses and cannot be used or re-defined for any other purpose in Basic. Keywords cannot be used as a variable name. Some of the keywords of Basic are...IF, THEN, WHILE etc..

    0 讨论(0)
  • 2020-12-13 06:25

    keyword, - a word with special meaning in a particular context. It's semantic definition.

    reserved word is a word that cannot be used as an identifier - such as, variable, and function name. It's syntactic definition.

    E.g.In Java, all keywords are reserved words. Probably not the reverse. goto is reserved word but not used and has no function.

    In older languages like FORTRAN there were keywords but no reserved words.

    However, keyword and reserved word are used interchangeably.

    0 讨论(0)
  • 2020-12-13 06:29

    Just to show that the distinction is very meaningful:

    Not in all languages are all keywords reserved words. In Fortran it is possible to do this:

    if if then then else else
    

    In this case, the keywords are not reserved, but depending on context can be interpreted by the compiler as variables.

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题