What is the difference between "keyword" and "reserved word"?

Language AgnosticProgramming LanguagesSyntaxKeywordReserved Words

Language Agnostic Problem Overview


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:

> This proposal introduces five new keywords: concept, concept map, where, axiom, and late check. All of these keywords will also be > reserved words.

Language Agnostic Solutions


Solution 1 - Language Agnostic

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

Solution 2 - Language Agnostic

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.

Solution 3 - Language Agnostic

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

Solution 4 - Language Agnostic

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

Solution 5 - Language Agnostic

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.

Solution 6 - Language Agnostic

  • 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

Solution 7 - Language Agnostic

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.

Solution 8 - Language Agnostic

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.

Solution 9 - Language Agnostic

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.

Solution 10 - Language Agnostic

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPiotr DobrogostView Question on Stackoverflow
Solution 1 - Language AgnosticmolfView Answer on Stackoverflow
Solution 2 - Language AgnosticRalph M. RickenbachView Answer on Stackoverflow
Solution 3 - Language AgnosticskaffmanView Answer on Stackoverflow
Solution 4 - Language AgnosticBhushan BhangaleView Answer on Stackoverflow
Solution 5 - Language AgnosticCodeClimberView Answer on Stackoverflow
Solution 6 - Language AgnosticSravanthiView Answer on Stackoverflow
Solution 7 - Language AgnosticanonView Answer on Stackoverflow
Solution 8 - Language AgnosticKSRKSRView Answer on Stackoverflow
Solution 9 - Language AgnosticYergalemView Answer on Stackoverflow
Solution 10 - Language AgnosticAqsa BajwaView Answer on Stackoverflow