What is the difference between "keyword" and "reserved word"?
Asked Answered
A

10

58

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.

Adrian answered 3/7, 2009 at 11:17 Comment(5)
@Piotr Note it is a bad idea to accept an answer until a few people have had a chance to respond. I recommend waiting at least 12 hours.Centillion
@Neil It's really a very good answer that got accepted :)Adrian
@Piotr I'm not criticising the answer. But if you know it is somehow "good" without comparing it with other, possibly diametrically opposed answers, why did you ask the question in the first place?Centillion
@Neil This is the case when you can tell the answer is good just reading it alone. Besides at the time I accepted the best answer there were already 2 different answers clearly worse.Adrian
OK well MDN uses the term reserved keywords, so I'm still confused.Centerpiece
P
71

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
Proximate answered 3/7, 2009 at 11:22 Comment(7)
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.Monocular
In Java goto is a reserved word but is not a keyword.Polston
@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?).Proximate
a reserved word is not a keyword (e.g. because it is reserved for future use) was very enlightening finding for me for the day.Token
Keyword(s) is currently in use by language where as reserved word(s) is not in use now by language and may become keyword in future. so reserved word is reserved for future use as keyword.Pail
goto is a keyword in Java.Gunther
SQL has pretty disjoint sets of keywords and reserved words. Here are the complete lists of both. I think it's a good example to understand the difference.Bondage
O
21

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.

Ombudsman answered 3/7, 2009 at 11:30 Comment(4)
Good example of not every keyword is a "reserved word". Thanks.Adrian
Funny. And that suggests that fortran compiler designers put extra efforts to make that happen just to let developers create messy code :PToken
Can also do if then then then = else else else = thenCranage
LOL!!.. That's crazyStern
H
11

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

Hanny answered 3/7, 2009 at 11:25 Comment(2)
Good example of not every "reserved word" is a keyword. Thanks.Adrian
goto is a keyword in Java.Gunther
P
5

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

Phene answered 3/7, 2009 at 11:20 Comment(1)
This is backwards: keywords cannot be used as a user-defined name (if, else, etc.)Pooley
P
1

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.

Pedroza answered 3/7, 2009 at 11:20 Comment(0)
F
1
  • 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
Felixfeliza answered 10/10, 2010 at 13:16 Comment(1)
Where do those necromancers come from? I wanna revive corpses, too!Fu
C
0

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.

Centillion answered 3/7, 2009 at 11:25 Comment(1)
There might soon be a distinct divide between keywords and (specific) reserved identifiers: C++17 drops the deprecated hint meaning of the identifier register but retains it as reserved, so people can't start calling variables that, and perhaps it will take on a new meaning later as auto (so mercifully) did.Jeanninejeans
E
0

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.

Ellora answered 24/9, 2016 at 15:18 Comment(0)
K
0

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.

Kristy answered 5/6, 2017 at 18:59 Comment(4)
goto is a keyword in Java. Keywords are syntactic.Gunther
for forward compatibility reserved words like goto may become keywords. But goto has no special meaning. Keywords do have meaning in the context they're used. Refer: en.wikipedia.org/wiki/Reserved_wordKristy
Wikipedia has exactly nothing to do with it. I am quoting from and citing the Java Language Specification. If Wikipedia disagrees with that, it's wrong, by definition.Gunther
Even the Java Language spec what you linked mentions goto as reserved word on bottom if you notice. It's C++ Keyword but for Java it's more expressive to say as reserved word for it's not in use. What do you mean by Kewords are syntactic? But reserved words are.Kristy
D
-1

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

Dorian answered 7/1, 2012 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.