Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Reserved word
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Word in a programming language that cannot be used as an identifier}} In a [[programming language]], a '''reserved word''' (sometimes known as a '''reserved identifier''') is a word that cannot be used by a [[programmer]] as an [[Identifier (computer languages)|identifier]], such as the name of a variable, function, or [[Label (programming language)|label]] – it is "reserved from use". In brief, an ''identifier'' starts with a letter, which is followed by any sequence of letters and digits (in some languages, the [[underscore]] '_' is treated as a letter). In an [[Imperative programming|imperative programming language]] and in many [[Object-oriented programming|object-oriented programming languages]], apart from assignments and subroutine calls, keywords are often used to identify a particular [[Statement (computer science)|statement]], e.g. '''if''', '''while''', '''do''', '''for''', etc. Many languages treat keywords as reserved words, including [[Ada (programming language)|Ada]], [[C (programming language)|C]], [[C++]], [[COBOL]], [[Java (programming language)|Java]], and [[Pascal (programming language)|Pascal]]. The number of reserved words varies widely from one language to another: C has about 30 while COBOL has about 400. A few languages do not have any reserved words; [[Fortran]] and [[PL/I]] identify keywords by context, while [[Algol 60]] and [[Algol 68]] generally use [[Stropping (syntax)|stropping]] to distinguish keywords from programmer-defined identifiers, e.g. <code>.if</code> or <code>'if</code> or <code>'if'</code> or <code><u>if</u></code>is a keyword distinct from identifier <code>if</code>. Most programming languages have a standard library (or libraries), e.g. mathematical functions sin, cos, etc. The names provided by a library are not reserved, and can be redefined by a programmer if the library functionality is not required. ==Distinction== When using an Interactive Development Environment (IDE) to develop a program, the IDE will generally highlight reserved words by displaying them in a different colour. In some IDEs, comments may also be highlighted (in yet another colour). This makes it easy for a programmer to notice unexpected use of a reserved word and/or failure to terminate a comment correctly. There may be reserved words which are not keywords. For example, in Java, <code>true</code> and <code>false</code> are reserved words used as Boolean (logical) literals. As another example, in Pascal, <code>div</code> and <code>mod</code> are reserved words used as operators (integer division and remainder). There may also be reserved words which have no defined meaning. For example, in Java, <code>goto</code> and <code>const</code> are listed as reserved words, but are not otherwise mentioned in the Java syntax rules. A keyword such as '''if''' or '''while''' is used during [[parser|syntax analysis]] to determine what sort of statement is being considered. Such analysis is much simpler if keywords are either reserved or stropped. Consider the complexity of using contextual analysis in Fortran 77 to distinguish: IF (B) l1,l2 ! two-way branch, where B is a boolean/logical expression IF (N) l1,l2,l3 ! three-way branch, where N is a numeric expression IF (B) THEN ! start conditional block IF (B) THEN = 3.1 ! conditional assignment to variable THEN IF (B) X = 10 ! single conditional statement IF (B) GOTO l4 ! conditional jump IF (N) = 2 ! assignment to a subscripted variable named IF PL/I can also allow some apparently confusing constructions: IF IF = THEN THEN ... /* (the second <code>IF</code> and the first <code>THEN</code> are variables */ ==Advantages== Programs may be more readable as compared with a programming language which uses stropping. It is easier for an IDE to highlight keywords, without having to do a contextual analysis to determine which words are actually keywords (see Fortran examples in previous section). A compiler can be faster, since it can quickly determine if a word is a keyword, without having to do a contextual analysis over the whole of an arbitrarily long statement. ==Disadvantages== A programming language may be difficult for new users to learn because of a (possibly long) list of reserved words to memorize which can't be used as identifiers. It may be difficult to extend the language because addition of reserved words for new features might invalidate existing programs or, conversely, "overloading" of existing reserved words with new meanings can be confusing. Porting programs can be problematic because a word not reserved by one system or compiler might be reserved by another. ==Further reservation== Beyond reserving specific lists of words, some languages reserve entire ranges of words, for use as private spaces for future language version, different dialects, [[compiler]] vendor-specific extensions, or for internal use by a compiler, notably in [[name mangling]]. This is most often done by using a prefix, often one or more [[underscore]]s. C and [[C++]] are notable in this respect: C99 reserves identifiers that start with two underscores or an underscore followed by an uppercase letter, and further reserves identifiers that start with a single underscore (in the ordinary and tag spaces) for use in [[file scope]];<ref>C99 specification, 7.1.3 Reserved identifiers</ref> with C++03 further reserves identifiers that contain a double underscore anywhere<ref>C++03 specification, 17.4.3.2.1 Global names [lib.global.names]</ref> – this allows the use of a double underscore as a separator (to connect user identifiers), for instance. The frequent use of a double underscores in internal identifiers in Python gave rise to the abbreviation ''dunder;'' this was coined by Mark Jackson<ref>{{cite mailing list |first=Mark |last=Jackson |date=September 26, 2002 |title=How do you pronounce "__" (double underscore)? |mailing-list=python-list |url=https://mail.python.org/pipermail/python-list/2002-September/155836.html |access-date=November 9, 2014}}</ref> and independently by Tim Hochberg,<ref>{{cite mailing list |title=How do you pronounce "__" (double underscore)? |first=Tim |last=Hochberg |date=Sep 26, 2002 |mailing-list=python-list |url=https://mail.python.org/pipermail/python-list/2002-September/157561.html |access-date=November 9, 2014}}</ref> within minutes of each other, both in reply to the same question in 2002.<ref>{{Cite web|url=https://wiki.python.org/moin/DunderAlias|title=DunderAlias - Python Wiki|website=wiki.python.org}}</ref><ref>{{cite mailing list |title=How do you pronounce "__" (double underscore)? |first=Pat |last=Notz |date=Sep 26, 2002 |mailing-list=python-list |url=https://mail.python.org/pipermail/python-list/2002-September/137816.html |access-date=November 9, 2014}}</ref> ==Specification== The list of reserved words and keywords in a language are defined when a language is developed, and both form part of a language's [[formal specification]]. Generally one wishes to minimize the number of reserved words, to avoid restricting valid identifier names. Further, introducing new reserved words breaks existing programs that use that word (it is not backwards compatible), so this is avoided. To prevent this and provide [[forward compatibility]], sometimes words are reserved without having a current use (a reserved word that is not a keyword), as this allows the word to be used in future without breaking existing programs. Alternatively, new language features can be implemented as predefineds, which can be overridden, thus not breaking existing programs. Reasons for flexibility include allowing compiler vendors to extend the specification by including non-standard features, different standard dialects of language to extend it, or future versions of the language to include additional features. For example, a procedural language may anticipate adding [[Object-oriented programming|object-oriented]] capabilities in a future version or some dialect, at which point one might add keywords like <code>class</code> or <code>object</code>. To accommodate this possibility, the current specification may make these reserved words, even if they are not currently used. A notable example is in [[Java (programming language)|Java]], where <code>const</code> and <code>goto</code> are reserved words — they have no meaning in Java but they also cannot be used as identifiers. By reserving the terms, they can be implemented in future versions of Java, if desired, without breaking older Java source code. For example, there was a proposal in 1999 to add C++-like <code>const</code> to the language, which was possible using the <code>const</code> word, since it was reserved but currently unused; however, this proposal was rejected – notably because even though adding the feature would not break any existing programs, using it in the standard library (notably in collections) ''would'' break compatibility.<ref>{{cite web|url=http://bugs.java.com/view_bug.do?bug_id=4211070 |title=Bug ID: JDK-4211070 Java should support const parameters (like C++) for code {{sic|maint|ainence|nolink=y}} |publisher=Bugs.sun.com |access-date=2014-11-04}}<!-- was previously at: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4211070 2013-08-18 --></ref> [[JavaScript]] also contains a number of reserved words without special functionality; the exact list varies by version and mode.<ref>{{Cite web|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar|title=Lexical grammar - JavaScript | MDN|website=developer.mozilla.org|date=8 November 2023 }}</ref> Languages differ significantly in how frequently they introduce new reserved words or keywords and how they name them, with some languages being very conservative and introducing new keywords rarely or never, to avoid breaking existing programs, while other languages introduce new keywords more freely, requiring existing programs to change existing identifiers that conflict. A case study is given by new keywords in [[C11 (C standard revision)|C11]] compared with [[C++11]], both from 2011 – recall that in C and C++, identifiers that begin with an underscore followed by an uppercase letter are reserved:<ref>C99 specification, 7.1.3 Reserved identifiers: "All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use."</ref> {{quote|The C committee prefers not to create new keywords in the user name space, as it is generally expected that each revision of C will avoid breaking older C programs. By comparison, the C++ committee (WG21) prefers to make new keywords as normal‐looking as the old keywords. For example, C++11 defines a new <code>thread_local</code> keyword to designate static storage local to one thread. C11 defines the new keyword as <code>_Thread_local.</code> In the new C11 header <threads.h>, there is a macro definition to provide the normal‐looking name:<ref>[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3631.pdf C11:The New C Standard], Thomas Plum, "A Note on Keywords"</ref> :<code>#define thread_local _Thread_local</code> }} That is, C11 introduced the keyword <code>_Thread_local</code> within an existing set of reserved words (those with a certain prefix), and then used a separate facility (macro processing) to allow its use as if it were a new keyword without any prefixing, while C++11 introduce the keyword <code>thread_local</code> despite this not being an existing reserved word, breaking any programs that used this, but without requiring macro processing. ==Reserved words and language independence== Microsoft's [[.NET Framework|.NET]] [[Common Language Infrastructure]] (CLI) specification allows code written in 40+ different programming languages to be combined into a final product. Because of this, identifier/reserved word collisions can occur when code implemented in one language tries to execute code written in another language. For example, a [[Visual Basic (.NET)]] library may contain a [[Class (computer programming)|class]] definition such as: <syntaxhighlight lang="vbnet"> ' Class Definition of This in Visual Basic.NET: Public Class this ' This class does something... End Class </syntaxhighlight> If this is compiled and distributed as part of a toolbox, a [[C Sharp (programming language)|C#]] programmer, wishing to define a variable of type "<code>this</code>" would encounter a problem: <code>'this'</code> is a reserved word in C#. Thus, the following will not compile in C#: <syntaxhighlight lang="csharp"> // Using This Class in C#: this x = new this(); // Won't compile! </syntaxhighlight> A similar issue arises when accessing members, overriding [[virtual method]]s, and identifying namespaces. This is resolved by [[Stropping (syntax)|stropping]]. To work around this issue, the specification allows placing (in C#) the [[at-sign]] before the identifier, which forces it to be considered an identifier rather than a reserved word by the compiler: <syntaxhighlight lang="csharp"> // Using This Class in C#: @this x = new @this(); // Will compile! </syntaxhighlight> For consistency, this use is also permitted in non-public settings such as local variables, parameter names, and private members. ==See also== * [[C syntax#Reserved keywords|C reserved keywords]] * [[C++_syntax#Keywords|C++ keywords]] * [[List of Java keywords]] * [[List of SQL reserved words]] * [[Symbol (programming)]] ==References== {{Reflist}} {{Refbegin}} {{Refend}} [[Category:Programming constructs]] [[Category:Programming language topics]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite mailing list
(
edit
)
Template:Cite web
(
edit
)
Template:Quote
(
edit
)
Template:Refbegin
(
edit
)
Template:Refend
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)