In a
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
, a reserved word (sometimes known as a reserved identifier) is a word that cannot be used by a
programmer
A programmer, computer programmer or coder is an author of computer source code someone with skill in computer programming.
The professional titles Software development, ''software developer'' and Software engineering, ''software engineer' ...
as an
identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, person, physical countable object (or class thereof), or physical mass ...
, such as the name of a variable, function, or
label
A label (as distinct from signage) is a piece of paper, plastic film, cloth, metal, or other material affixed to a container or product. Labels are most often affixed to packaging and containers using an adhesive, or sewing when affix ...
– 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
An underscore or underline is a line drawn under a segment of text. In proofreading, underscoring is a convention that says "set this text in italic type", traditionally used on manuscript or typescript as an instruction to the printer. Its ...
'_' is treated as a letter).
In an
imperative programming language
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program con ...
and in many
object-oriented programming languages
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impleme ...
, apart from assignments and subroutine calls, keywords are often used to identify a particular
statement
Statement or statements may refer to: Common uses
*Statement (computer science), the smallest standalone element of an imperative programming language
*Statement (logic and semantics), declarative sentence that is either true or false
*Statement, ...
, e.g. if, while, do, for, etc. Many languages treat keywords as reserved words, including
Ada,
C,
C++,
COBOL
COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural, and, since 2002, object-oriented language. COBOL is primarily ...
,
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, and
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
PL/I (Programming Language One, pronounced and sometimes written PL/1) is a procedural, imperative computer programming language initially developed by IBM. It is designed for scientific, engineering, business and system programming. It has b ...
identify keywords by context, while
Algol 60
ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a ...
and
Algol 68
ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language member of the ALGOL family that was conceived as a successor to the ALGOL 60 language, designed with the goal of a much wider scope of application and ...
generally use
stropping to distinguish keywords from programmer-defined identifiers, e.g.
.if
or
'if
or
'if'
or
if
is a keyword distinct from identifier
if
.
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,
true
and
false
are reserved words used as Boolean (logical) literals. As another example, in Pascal,
div
and
mod
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,
goto
and
const
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
syntax analysis
In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituency) ...
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
IF
and the first
THEN
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
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
vendor-specific extensions, or for internal use by a compiler, notably in
name mangling
In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.
It provides means to e ...
.
This is most often done by using a prefix, often one or more
underscore
An underscore or underline is a line drawn under a segment of text. In proofreading, underscoring is a convention that says "set this text in italic type", traditionally used on manuscript or typescript as an instruction to the printer. Its ...
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; with C++03 further reserves identifiers that contain a double underscore anywhere – 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 and independently by Tim Hochberg, within minutes of each other, both in reply to the same question in 2002.
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
In computer science, formal specifications are mathematically based techniques whose purpose is to help with the implementation of systems and software. They are used to describe a system, to analyze its behavior, and to aid in its design by verify ...
. 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
Forward compatibility or upward compatibility is a design characteristic that allows a system to accept input intended for a later version of itself. The concept can be applied to entire systems, electrical interfaces, telecommunication signal ...
, 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
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impleme ...
capabilities in a future version or some dialect, at which point one might add keywords like
class
or
object
. 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
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, where
const
and
goto
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
const
to the language, which was possible using the
const
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.
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
also contains a number of reserved words without special functionality; the exact list varies by version and mode.
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 compared with
C++11
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
, both from 2011 – recall that in C and C++, identifiers that begin with an underscore followed by an uppercase letter are reserved:
[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."]
That is, C11 introduced the keyword
_Thread_local
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
thread_local
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
The .NET platform (pronounced as "''dot net"'') is a free and open-source, managed code, managed computer software framework for Microsoft Windows, Windows, Linux, and macOS operating systems. The project is mainly developed by Microsoft emplo ...
Common Language Infrastructure
The Common Language Infrastructure (CLI) is an open specification and technical standard originally developed by Microsoft and standardized by International Organization for Standardization, ISO/International Electrotechnical Commission, IEC (ISO/ ...
(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
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
definition such as:
' Class Definition of This in Visual Basic.NET:
Public Class this
' This class does something...
End Class
If this is compiled and distributed as part of a toolbox, a
C# programmer, wishing to define a variable of type "
this
" would encounter a problem:
'this'
is a reserved word in C#. Thus, the following will not compile in C#:
// Using This Class in C#:
this x = new this(); // Won't compile!
A similar issue arises when accessing members, overriding
virtual methods, and identifying namespaces.
This is resolved by
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:
// Using This Class in C#:
@this x = new @this(); // Will compile!
For consistency, this use is also permitted in non-public settings such as local variables, parameter names, and private members.
See also
*
C reserved keywords
*
C++ keywords
*
List of Java keywords
In the Java programming language, a keyword is any one of 68 reserved words that have a predefined meaning in the language. Because of this, programmers cannot use keywords in some contexts, such as names for variables, methods, classes, or a ...
*
List of SQL reserved words
*
Symbol (programming)
References
{{Refend
Programming constructs
Programming language topics