Increment and decrement operators are
unary operators that increase or decrease their
operand by one.
They are commonly found in
imperative 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 ...
s.
C-like languages feature two versions (pre- and post-) of each operator with slightly different semantics.
In languages syntactically derived from
B (including C and its various derivatives), the increment operator is written as
++
and the decrement operator is written as
--
. Several other languages use inc(x) and dec(x) functions.
The increment operator increases, and the decrement operator decreases, the value of its operand by 1. The operand must have an arithmetic or
pointer data type
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
, and must refer to a modifiable
data object
In software development, an object is an entity that has state, behavior, and identity. An object can model some part of reality or can be an invention of the design process whose collaborations with other such objects serve as the mechanisms t ...
. Pointers values are increased (or decreased) by an amount that makes them point to the next (or previous) element adjacent in memory.
In languages that support both versions of the operators:
* The ''pre''-increment and ''pre''-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value.
* The ''post''-increment and ''post''-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's value ''prior'' to the increment (or decrement) operation.
In languages where increment/decrement is not an expression (e.g.,
Go), only one version is needed (in the case of Go, post operators only).
Since the increment/decrement operator modifies its operand, use of such an operand more than once within the same expression can produce undefined results. For example, in expressions such as
x - ++x
, it is not clear in what sequence the subtraction and increment operations should be performed. Such expressions generally invoke
undefined behavior, and should be avoided.
In languages with typed pointers like C,
the increment operator steps the pointer to the next item of that type -- increasing the value of the pointer by the size of that type.
When a pointer (of the right type) points to any item in an array, incrementing (or decrementing) makes the pointer point to the "next" (or "previous") item of that array.
Thus, incrementing a pointer to an integer makes it point to the next integer (typically increasing the pointer value by 4);
incrementing a pointer to a structure of size 106 bytes makes it point to the next structure by increasing the pointer value by 106.
Examples
The following C code fragment illustrates the difference between the ''pre'' and ''post'' increment and decrement operators:
int x;
int y;
// Increment operators
// Pre-increment: x is incremented by 1, then y is assigned the value of x
x = 1;
y = ++x; // x is now 2, y is also 2
// Post-increment: y is assigned the value of x, then x is incremented by 1
x = 1;
y = x++; // y is 1, x is now 2
// Decrement operators
// Pre-decrement: x is decremented by 1, then y is assigned the value of x
x = 1;
y = --x; // x is now 0, y is also 0
// Post-decrement: y is assigned the value of x, then x is decremented by 1
x = 1;
y = x--; // y is 1, x is now 0
In languages lacking these operators, equivalent results require an extra line of code:
# Pre-increment: y = ++x
x = 1
x = x + 1 # x is now 2 (can be written as "x += 1" in Python)
y = x # y is also 2
# Post-increment: y = x++
x = 1
y = x # y is 1
x = x + 1 # x is now 2
The post-increment operator is commonly used with
array subscripts. For example:
// Sum the elements of an array
float sum_elements(float arr[], int n)
The post-increment operator is also commonly used with pointer (computer programming), pointers:
// Copy one array to another
void copy_array(float *src, float *dst, int n)
These examples also work in other C-like languages, such as
C++,
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
C#.
*Increment operator can be demonstrated by an example:
#include
int main()
**Output:
2
4
Supporting languages
The following list, though not complete or all-inclusive, lists some of the major programming languages that support the increment and decrement operators.
*
AWK
*
Bash
*
C
*
C++
*
C#
*
CFML
*
D
*
Go
*
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 ...
*
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 ...
*
Objective-C
Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was ...
*
GNU Octave
*
PARI/GP
*
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
*
PHP
*
PowerShell
PowerShell is a shell program developed by Microsoft for task automation and configuration management. As is typical for a shell, it provides a command-line interpreter for interactive use and a script interpreter for automation via a langu ...
*
Vala
* Vex, a scripting language in
Houdini
*
Wolfram Language
Apple's
Swift
Swift or SWIFT most commonly refers to:
* SWIFT, an international organization facilitating transactions between banks
** SWIFT code
* Swift (programming language)
* Swift (bird), a family of birds
It may also refer to:
Organizations
* SWIF ...
once supported these operators, but they have been depreciated since version 2.2 and removed as of version 3.0.
Pascal,
Delphi
Delphi (; ), in legend previously called Pytho (Πυθώ), was an ancient sacred precinct and the seat of Pythia, the major oracle who was consulted about important decisions throughout the ancient Classical antiquity, classical world. The A ...
,
Modula-2, and
Oberon
Oberon () is a king of the fairy, fairies in Middle Ages, medieval and Renaissance literature. He is best known as a character in William Shakespeare's play ''A Midsummer Night's Dream'', in which he is King of the Fairies and spouse of Titania ...
uses functions (
inc(x)
and
dec(x)
) instead of operators.
Tcl uses the
incr
command.
Notably
Python and
Rust do not support these operators.
History
The concept was introduced in the
B programming language circa 1969 by
Ken Thompson
Kenneth Lane Thompson (born February 4, 1943) is an American pioneer of computer science. Thompson worked at Bell Labs for most of his career where he designed and implemented the original Unix operating system. He also invented the B (programmi ...
.
Thompson went a step further by inventing the ++ and -- operators, which increment or decrement; their prefix or postfix position determines whether the alteration occurs before or after noting the value of the operand. They were not in the earliest versions of B, but appeared along the way. People often guess that they were created to use the auto-increment and auto-decrement address modes provided by the DEC PDP-11 on which C and Unix first became popular. This is historically impossible, since there was no PDP-11 when B was developed. The PDP-7, however, did have a few 'auto-increment' memory cells, with the property that an indirect memory reference through them incremented the cell. This feature probably suggested such operators to Thompson; the generalization to make them both prefix and postfix was his own. Indeed, the auto-increment cells were not used directly in implementation of the operators, and a stronger motivation for the innovation was probably his observation that the translation of ++x was smaller than that of x=x+1.
See also
*
Augmented assignment – for
+=
and
-=
operators
*
PDP-7
*
PDP-11
*
Successor function
In mathematics, the successor function or successor operation sends a natural number to the next one. The successor function is denoted by ''S'', so ''S''(''n'') = ''n'' +1. For example, ''S''(1) = 2 and ''S''(2) = 3. The successor functio ...
References
{{Reflist
Operators (programming)
Unary operations