views:

1894

answers:

7

What's the difference between keyword and reserved word?

Fore example in the concepts' proposal 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.

A: 

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.

CodeClimber
+3  A: 

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

Bhushan
+10  A: 

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
molf
All keywords are reserved words, but not all reserved words are keywords although the difference is rather vague. Some languages have directives which have a special meaning based on the context. And can be used as identifiers on other places.
Gamecat
In Java goto is a reserved word but is not a keyword.
pjp
@Gamecat, no this is not true. There is a fundamental difference, and there are even real-world examples where keywords are not reserved words (Fortran, Postscript, ...others?).
molf
A: 

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.

anon
+5  A: 

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

skaffman
Good example of not every "reserved word" is a keyword. Thanks.
Piotr Dobrogost
+7  A: 

Just to show that the distinction is very meaningfull:

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.

Ralph Rickenbach
Good example of not every keyword is a "reserved word". Thanks.
Piotr Dobrogost
A: 

Keyword:It has some meaning nd we can use in program. Reserved word:We con't use in program.They may be used in future. Ex:goto

Sravanthi
Where do those necromancers come from? I wanna revive corpses, too!
delnan