The following C code fragment illustrates the difference between the pre and post increment and decrement operators:
In languages lacking these operators, equivalent results require an extra line of code:
The post-increment operator is commonly used with array subscripts. For example:
The post-increment operator is also commonly used with pointers:
These examples also work in other C-like languages, such as C++, Java, and C#.
The following list, though not complete or all-inclusive, lists some of the major programming languages that support the increment and decrement operators.
Apple's Swift once supported these operators, but they have been depreciated since version 2.213 and removed as of version 3.0.1415
Pascal, Delphi, Modula-2, and Oberon uses functions (inc(x) and dec(x)) instead of operators. Tcl uses the incr command.
Notably Python and Rust do not support these operators.
The concept was introduced in the B programming language circa 1969 by Ken Thompson.16
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.
Richard M Reese. "Understanding and Using C Pointers". "Chapter 4. Pointers and Arrays". O'Reilly Media, Inc. 2013. ISBN 9781449344184 https://books.google.com/books?id=-U155tRMLJgC ↩
Richard Petersen. "Introductory C with C++". 2019. Figure 12-12. https://www.google.com/books/edition/Introductory_C_with_C++/euCvDwAAQBAJ ↩
"GNU Awk's User Guide". Free Software Foundation. https://www.gnu.org/software/gawk/manual/html_node/Increment-Ops.html ↩
"8.3. The Double-Parentheses Construct". The Linux Documentation Project. http://tldp.org/LDP/abs/html/dblparens.html ↩
Ritchie, Brian W. Kernighan; Dennis M.; Ritchie, Dennis (1988). The C programming language (2. ed., [Nachdr.] ed.). Englewood Cliffs, N.J.: Prentice Hall. p. 18. ISBN 0-13-110362-8.{{cite book}}: CS1 maint: multiple names: authors list (link) 0-13-110362-8 ↩
"Increment/decrement operators". cppreference.com. http://en.cppreference.com/w/cpp/language/operator_incdec ↩
"++ Operator (C# Reference)". Microsoft Developer Network. 8 April 2023. http://msdn.microsoft.com/en-us/library/36x43w8w.aspx ↩
"Operator Overloading". dlang.org. https://dlang.org/spec/operatoroverloading.html ↩
"GP Operators and their Priorities". https://pari.math.u-bordeaux.fr/dochtml/html-stable/operators.html ↩
"About Assignment Operators". 19 January 2024. https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_assignment_operators ↩
"Increment Wolfram Language Symbol". Wolfram Language Documentation Center. https://reference.wolfram.com/language/ref/Increment.html ↩
"Decrement Wolfram Language Symbol". Wolfram Language Documentation Center. https://reference.wolfram.com/language/ref/Decrement.html ↩
"New Features in Swift 2.2". Swift Official Website. 30 March 2016. https://www.swift.org/blog/swift-2.2-new-features/#-and----are-deprecated ↩
"Swift 3.0 Released!". Swift Official Website. 13 September 2016. https://www.swift.org/blog/swift-3.0-released/ ↩
"Remove the ++ and -- operators". Swift evolution. https://github.com/swiftlang/swift-evolution/blob/main/proposals/0004-remove-pre-post-inc-decrement.md ↩
Ritchie, Dennis M. (March 1993). "The Development of the C Language". ACM SIGPLAN Notices. 28 (3): 5. doi:10.1145/155360.155580. /wiki/Dennis_Ritchie ↩