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

房东的猫 提交于 2019-11-28 17:44:50

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
Ralph M. Rickenbach

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.

A good example of this distinction is "goto" in Java. It's not a language keyword (i.e. it's not valid Java), but it is a reserved word.

It seems that the java designers are telling us "We're not going to use 'goto', and neither are you".

Wiki says this "A keyword is a word that is special only in certain contexts but a reserved word is a special word that cannot be used as a user-defined name."

http://en.wikipedia.org/wiki/Reserved_word#Reserved_word_vs._keyword

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.

Sravanthi
  • 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

Really it will depend a lot on context. For example, the ISO C++ Standard says that things like "if", "while", "int" etc. are keywords, and doesn't actually use the term reserved word, except once, in a footnote, where something else was probably meant :-)

The standard doe specify reserved names - for example, all names that begin with an underscore and an uppercase letter are reserved names.

Keywords : Keywords has some special functionalities to the compiler. So the keywords can not be used as identifiers in coding. Reserved words: Reserve words are the words which are reserved for future use. In java, const and goto are the reserved words which are not being used currently and may be brought back to java in the future. If we check here Java Language Keywords (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html) , It says that java (latest I guess) has 50 keywords including goto and const. So goto and const are the keywords which are reserved.

Aqsa Bajwa

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..

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!