Increment and decrement operators explained

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 ++ 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, 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:

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);[1] incrementing a pointer to a structure of size 106 bytes makes it point to the next structure by increasing the pointer value by 106.[2]

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 xx = 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 1x = 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 xx = 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 1x = 1;y = x--; // y is 1, x is now 0

In languages lacking these operators, equivalent results require an extra line of code:

  1. Pre-increment: y = ++x

x = 1x = x + 1 # x is now 2 (can be written as "x += 1" in Python)y = x # y is also 2

  1. Post-increment: y = x++

x = 1y = x # y is 1x = x + 1 # x is now 2

The post-increment operator is commonly used with array subscripts. For example:// Sum the elements of an arrayfloat sum_elements(float arr[], int n)

The post-increment operator is also commonly used with pointers:// Copy one array to anothervoid copy_array(float *src, float *dst, int n)

These examples also work in other C-like languages, such as C++, Java, and C#.

  1. include

int main

24

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.

Apple's Swift once supported these operators, but they have been depreciated since version 2.2[13] and removed as of version 3.0.[14] [15]

Pascal, Delphi, Modula-2, and Oberon uses functions (inc(x) and dec(x)) instead of operators.

Notably Python and Rust do not support these operators.

History

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.

See also

Notes and References

  1. Richard M Reese."Understanding and Using C Pointers"."Chapter 4. Pointers and Arrays".O'Reilly Media, Inc.2013.
  2. Richard Petersen."Introductory C with C++".2019.Figure 12-12.
  3. Web site: GNU Awk's User Guide. Free Software Foundation.
  4. Web site: 8.3. The Double-Parentheses Construct. The Linux Documentation Project.
  5. Book: Ritchie. Brian W. Kernighan; Dennis M.. Ritchie. Dennis. The C programming language. 1988. Prentice Hall. Englewood Cliffs, N.J.. 0-13-110362-8. 18. 2. ed., [Nachdr.]. registration.
  6. Web site: Increment/decrement operators. cppreference.com.
  7. Web site: ++ Operator (C# Reference). Microsoft Developer Network.
  8. Web site: Operator Overloading. dlang.org.
  9. Web site: GP Operators and their Priorities.
  10. Web site: About Assignment Operators.
  11. Web site: Increment Wolfram Language Symbol. Wolfram Language Documentation Center.
  12. Web site: Decrement Wolfram Language Symbol. Wolfram Language Documentation Center.
  13. Web site: New Features in Swift 2.2. Swift Official Website.
  14. Web site: Swift 3.0 Released!. Swift Official Website.
  15. Web site: Remove the ++ and -- operators. Swift evolution.
  16. Dennis M.. Ritchie . Dennis Ritchie . The Development of the C Language . March 1993 . ACM SIGPLAN Notices . 28 . 3 . 5 . 10.1145/155360.155580 . free .