Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Increment/decrement operators

      From cppreference.com
      <c‎ |language
       
       
       
       

      Increment/decrement operators are unary operators that increment/decrement the value of a variable by 1.

      They can have postfix form:

      expr++
      expr--

      As well as the prefix form:

      ++expr
      --expr

      The operandexpr of both prefix and postfix increment or decrement must be amodifiable lvalue ofinteger type (including_Bool and enums), real floating type, or a pointer type. It may be cvr-qualified, unqualified, oratomic.

      The result of the postfix increment and decrement operators is the value ofexpr.

      The result of the prefix increment operator is the result of adding the value1 to the value ofexpr: the expression++e is equivalent toe+=1. The result of the prefix decrement operator is the result of subtracting the value1 from the value ofexpr: the expression--e is equivalent toe-=1.

      Increment operators initiate the side-effect of adding the value1 of appropriate type to the operand. Decrement operators initiate the side-effect of subtracting the value1 of appropriate type from the operand. As with any other side-effects, these operations complete at or before the nextsequence point.

      int a=1;int b= a++;// stores 1+a (which is 2) to a// returns the old value of a (which is 1)// After this line, b == 1 and a == 2a=1;int c=++a;// stores 1+a (which is 2) to a// returns 1+a (which is 2)// after this line, c == 2 and a == 2

      Post-increment or post-decrement on anyatomic variable is an atomic read-modify-write operation with memory ordermemory_order_seq_cst.

      (since C11)

      Seearithmetic operators for limitations on pointer arithmetic, as well as for implicit conversions applied to the operands.

      Contents

      [edit]Notes

      Because of the side-effects involved, increment and decrement operators must be used with care to avoid undefined behavior due to violations ofsequencing rules.

      Increment/decrement operators are not defined for complex or imaginary types: the usual definition of adding/subtracting the real number 1 would have no effect on imaginary types, and making it add/subtracti for imaginaries but1 for complex numbers would have made it handle0+yi different fromyi.

      Unlike C++ (and some implementations of C), the increment/decrement expressions are never themselves lvalues:&++a is invalid.

      [edit]Example

      Run this code
      #include <stdio.h>#include <stdlib.h> int main(void){int a=1;int b=1; printf("original values: a == %d, b == %d\n", a, b);printf("result of postfix operators: a++ == %d, b-- == %d\n", a++, b--);printf("after postfix operators applied: a == %d, b == %d\n", a, b);printf("\n"); // Reset a and b.    a=1;    b=1; printf("original values: a == %d, b == %d\n", a, b);printf("result of prefix operators: ++a == %d, --b == %d\n",++a,--b);printf("after prefix operators applied: a == %d, b == %d\n", a, b);}

      Output:

      original values: a == 1, b == 1result of postfix operators: a++ == 1, b-- == 1after postfix operators applied: a == 2, b == 0 original values: a == 1, b == 1result of prefix operators: ++a == 2, --b == 0after prefix operators applied: a == 2, b == 0

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.5.2.4 Postfix increment and decrement operators (p: TBD)
      • 6.5.3.1 Prefix increment and decrement operators (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.5.2.4 Postfix increment and decrement operators (p: TBD)
      • 6.5.3.1 Prefix increment and decrement operators (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.5.2.4 Postfix increment and decrement operators (p: 85)
      • 6.5.3.1 Prefix increment and decrement operators (p: 88)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.5.2.4 Postfix increment and decrement operators (p: 75)
      • 6.5.3.1 Prefix increment and decrement operators (p: 78)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.3.2.4 Postfix increment and decrement operators
      • 3.3.3.1 Prefix increment and decrement operators

      [edit]See also

      Operator precedence

      Common operators
      assignmentincrement
      decrement
      arithmeticlogicalcomparisonmember
      access
      other

      a= b
      a+= b
      a-= b
      a*= b
      a/= b
      a%= b
      a&= b
      a|= b
      a^= b
      a<<= b
      a>>= b

      ++a
      --a
      a++
      a--

      +a
      -a
      a+ b
      a- b
      a* b
      a/ b
      a% b
      ~a
      a& b
      a| b
      a^ b
      a<< b
      a>> b

      !a
      a&& b
      a|| b

      a== b
      a!= b
      a< b
      a> b
      a<= b
      a>= b

      a[b]
      *a
      &a
      a->b
      a.b

      a(...)
      a, b
      (type) a
      a? b: c
      sizeof


      _Alignof
      (since C11)
      (until C23)

      alignof
      (since C23)

      C++ documentation forIncrement/decrement operators
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/operator_incdec&oldid=153216"

      [8]ページ先頭

      ©2009-2025 Movatter.jp