{{Short description|Unary operators that add or subtract one from their operand, respectively}} {{Refimprove|date=September 2014}} '''Increment''' and '''decrement operators''' are unary operators that increase or decrease their operand by one.
They are commonly found in imperative programming languages. 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 <code>++</code> and the decrement operator is written as <code>--</code>. 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, and must refer to a modifiable data object. 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 <code style="white-space:nowrap">x - ++x</code>, 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);<ref> Richard M Reese. [https://books.google.com/books?id=-U155tRMLJgC "Understanding and Using C Pointers"]. [https://www.oreilly.com/library/view/understanding-and-using/9781449344535/ch04.html "Chapter 4. Pointers and Arrays"]. O'Reilly Media, Inc. 2013. {{ISBN|9781449344184}} </ref> incrementing a pointer to a structure of size 106 bytes makes it point to the next structure by increasing the pointer value by 106.<ref> Richard Petersen. [https://www.google.com/books/edition/Introductory_C_with_C++/euCvDwAAQBAJ "Introductory C with C++"]. 2019. Figure 12-12. </ref>
==Examples== The following C code fragment illustrates the difference between the ''pre'' and ''post'' increment and decrement operators: <syntaxhighlight lang="c"> 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 </syntaxhighlight>
In languages lacking these operators, equivalent results require an extra line of code: <syntaxhighlight lang="python"> # 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 </syntaxhighlight>
The post-increment operator is commonly used with array subscripts. For example: <syntaxhighlight lang=C> // Sum the elements of an array float sum_elements(float arr[], int n) { float sum = 0.0; int i = 0;
while (i < n) sum += arr[i++]; // Post-increment of i, which steps // through n elements of the array return sum; } </syntaxhighlight>
The post-increment operator is also commonly used with pointers: <syntaxhighlight lang="c"> // Copy one array to another void copy_array(float *src, float *dst, int n) { while (n-- > 0) // Loop that counts down from n to zero *dst++ = *src++; // Copies element *(src) to *(dst), // then increments both pointers } </syntaxhighlight>
These examples also work in other C-like languages, such as C++, Java, and C#.
*Increment operator can be demonstrated by an example:<syntaxhighlight lang="c"> #include <stdio.h> int main() { int c = 2; printf("%d\n", c++); // this statement displays 2, then c is incremented by 1 to 3. printf("%d", ++c); // this statement increments c by 1, then c is displayed. return 0; } </syntaxhighlight> **Output:<syntaxhighlight lang="output"> 2 4 </syntaxhighlight>
==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.
{{col-begin}} {{col-break|width=25%}} * AWK<ref>{{cite web|title=GNU Awk's User Guide|url=https://www.gnu.org/software/gawk/manual/html_node/Increment-Ops.html|publisher=Free Software Foundation}}</ref> * Bash<ref>{{cite web|title=8.3. The Double-Parentheses Construct|url=http://tldp.org/LDP/abs/html/dblparens.html|publisher=The Linux Documentation Project}}</ref> * C<ref>{{cite book|last1=Ritchie|first1=Brian W. Kernighan; Dennis M.|last2=Ritchie|first2=Dennis|title=The C programming language|date=1988|publisher=Prentice Hall|location=Englewood Cliffs, N.J.|isbn=0-13-110362-8|page=[https://archive.org/details/cprogramminglang00bria/page/18 18]|edition=2. ed., [Nachdr.]|url-access=registration|url=https://archive.org/details/cprogramminglang00bria/page/18}}</ref> <!-- Someone more familiar with citation syntax should take a look at the warning this reference is causing. --> * C++<ref>{{cite web|title=Increment/decrement operators|url=http://en.cppreference.com/w/cpp/language/operator_incdec|publisher=cppreference.com}}</ref> * C#<ref>{{cite web|title=++ Operator (C# Reference)|date=8 April 2023 |url=http://msdn.microsoft.com/en-us/library/36x43w8w.aspx|publisher=Microsoft Developer Network}}</ref> * CFML * D<ref>{{cite web|title=Operator Overloading|url=https://dlang.org/spec/operatoroverloading.html|publisher=dlang.org}}</ref> {{col-break|width=25%}} * Go * Java * JavaScript * Objective-C * GNU Octave * PARI/GP<ref>{{cite web|title=GP Operators and their Priorities|url=https://pari.math.u-bordeaux.fr/dochtml/html-stable/operators.html}}</ref> {{col-break}} * Perl * PHP * PowerShell<ref>{{cite web|title=About Assignment Operators|date=19 January 2024 |url=https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_assignment_operators}}</ref> * Vala * Vex, a scripting language in Houdini * Wolfram Language<ref>{{cite web|title=Increment Wolfram Language Symbol|url=https://reference.wolfram.com/language/ref/Increment.html|publisher=Wolfram Language Documentation Center}}</ref><ref>{{cite web|title=Decrement Wolfram Language Symbol|url=https://reference.wolfram.com/language/ref/Decrement.html|publisher=Wolfram Language Documentation Center}}</ref>{{col-end}}
Apple's Swift once supported these operators, but they have been depreciated since version 2.2<ref>{{cite web|title=New Features in Swift 2.2|date=30 March 2016 |url=https://www.swift.org/blog/swift-2.2-new-features/#-and----are-deprecated |publisher=Swift Official Website}}</ref> and removed as of version 3.0.<ref>{{cite web|title=Swift 3.0 Released!|date=13 September 2016 |url=https://www.swift.org/blog/swift-3.0-released/|publisher=Swift Official Website}}</ref><ref>{{cite web|url=https://github.com/swiftlang/swift-evolution/blob/main/proposals/0004-remove-pre-post-inc-decrement.md|title=Remove the <code>++</code> and <code>--</code> operators|website=Swift evolution}}</ref>
Pascal, Delphi, Modula-2, and Oberon uses functions (<code>inc(x)</code> and <code>dec(x)</code>) instead of operators. Tcl uses the <code>incr</code> command.
Notably Python, Ruby and Rust do not support these operators.
==History==
The concept was introduced in the B programming language circa 1969 by Ken Thompson.<ref name="sigplan">{{cite journal | first = Dennis M.| last = Ritchie | author-link = Dennis Ritchie | title = The Development of the C Language | date = March 1993 | journal = ACM SIGPLAN Notices | volume = 28 | issue = 3 | page = 5 | url = http://www.bell-labs.com/usr/dmr/www/chist.html | doi = 10.1145/155360.155580 | doi-access = free }}</ref>
<blockquote>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.</blockquote>
==See also== * Augmented assignment – for <code>+=</code> and <code>-=</code> operators * PDP-7 * PDP-11 * Successor function
==References== {{Reflist}}
Category:Operators (programming) Category:Unary operations