Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Assignment operators

      From cppreference.com
      <c‎ |language
       
       
       
       

      Assignment and compound assignment operators are binary operators that modify the variable to their left using the value to their right.

      Operator Operator name Example Description Equivalent of
      = basic assignmenta= ba becomes equal tobN/A
      += addition assignmenta+= ba becomes equal to the addition ofa andba= a+ b
      -= subtraction assignmenta-= ba becomes equal to the subtraction ofb fromaa= a- b
      *= multiplication assignmenta*= ba becomes equal to the product ofa andba= a* b
      /= division assignmenta/= ba becomes equal to the division ofa byba= a/ b
      %= modulo assignmenta%= ba becomes equal to the remainder ofa divided byba= a% b
      &= bitwise AND assignmenta&= ba becomes equal to the bitwise AND ofa andba= a& b
      |= bitwise OR assignmenta|= ba becomes equal to the bitwise OR ofa andba= a| b
      ^= bitwise XOR assignmenta^= ba becomes equal to the bitwise XOR ofa andba= a^ b
      <<= bitwise left shift assignmenta<<= ba becomes equal toa left shifted byba= a<< b
      >>= bitwise right shift assignmenta>>= ba becomes equal toa right shifted byba= a>> b

      Contents

      [edit]Simple assignment

      The simple assignment operator expressions have the form

      lhs=rhs

      where

      lhs -modifiable lvalue expression of any complete object type
      rhs - expression of any typeimplicitly convertible tolhs orcompatible withlhs

      Assignment performsimplicit conversion from the value ofrhs to the type oflhs and then replaces the value in the object designated bylhs with the converted value ofrhs.

      Assignment also returns the same value as what was stored inlhs (so that expressions such asa= b= c are possible). Thevalue category of the assignment operator is non-lvalue (so that expressions such as(a=b)=c are invalid).

      rhs andlhs must satisfy one of the following:

      • bothlhs andrhs havearithmetic types, in which caselhs may bevolatile-qualified oratomic(since C11)
      • bothlhs andrhs havepointer tocompatible (ignoring qualifiers) types, or one of the pointers is a pointer to void, and theconversion would not add qualifiers to the pointed-to type.lhs may bevolatile orrestrict(since C99)-qualified oratomic(since C11).
      • lhs is a (possibly qualified or atomic(since C11)) pointer andrhs is a null pointer constant such asNULLor anullptr_t value(since C23)
      • lhs has type (possibly qualified or atomic(since C11))_Bool andrhs is a pointeror anullptr_t value(since C23)
      (since C99)
      (since C23)

      [edit]Notes

      Ifrhs andlhs overlap in memory (e.g. they are members of the same union), the behavior is undefined unless the overlap is exact and the types arecompatible.

      Although arrays are not assignable, an array wrapped in a struct is assignable to another object of the same (or compatible) struct type.

      The side effect of updatinglhs issequenced after the value computations, but not the side effects oflhs andrhs themselves and the evaluations of the operands are, as usual, unsequenced relative to each other (so the expressions such asi=++i; are undefined)

      Assignment strips extra range and precision from floating-point expressions (seeFLT_EVAL_METHOD).

      In C++, assignment operators are lvalue expressions, not so in C.

      Run this code
      #include <stdio.h> int main(void){// integersint i=1, j=2, k=3;// initialization, not assignment     i= j= k;// values of i and j are now 3//  (i = j) = k; // Error: lvalue requiredprintf("%d %d %d\n", i, j, k); // pointersconstchar c='A';// initialization; not assignmentconstchar*p=&c;// initialization; not assignmentconstchar**cpp=&p;// initialization; not assignment //  cpp = &p;   // Error: char** is not convertible to const char***cpp=&c;// OK, char* is convertible to const char*printf("%c\n",**cpp);    cpp=0;// OK, null pointer constant is convertible to any pointer // arraysint arr1[2]={1,2}, arr2[2]={3,4};//  arr1 = arr2; // Error: cannot assign to an arrayprintf("arr1[0]=%d arr1[1]=%d arr2[0]=%d arr2[1]=%d\n",            arr1[0],   arr1[1],   arr2[0],   arr2[1]); struct{int arr[2];} sam1={{5,6}}, sam2={{7,8}};    sam1= sam2;// OK: can assign arrays wrapped in structs printf("%d %d\n", sam1.arr[0], sam1.arr[1]);}

      Output:

      3 3 3Aarr1[0]=1 arr1[1]=2 arr2[0]=3 arr2[1]=47 8

      [edit]Compound assignment

      The compound assignment operator expressions have the form

      lhsoprhs

      where

      op - one of*=,/=%=,+=-=,<<=,>>=,&=,^=,|=
      lhs,rhs - expressions witharithmetic types (wherelhs may be qualified or atomic), except whenop is+= or-=, which also accept pointer types with the same restrictions as + and -

      The expressionlhs@=rhs is exactly the same aslhs=lhs@(rhs), except thatlhs is evaluated only once.

      Iflhs hasatomic type, the operation behaves as a single atomic read-modify-write operation with memory ordermemory_order_seq_cst.

      For integer atomic types, the compound assignment@= is equivalent to:

      T1* addr=&lhs;T2 val= rhs;T1 old=*addr;T1 new;do{ new= old @ val}while(!atomic_compare_exchange_strong(addr,&old, new);
      (since C11)
      Run this code
      #include <stdio.h> int main(void){int x=10;int hundred=100;int ten=10;int fifty=50;  printf("%d %d %d %d\n", x, hundred, ten, fifty);     hundred*= x;     ten/= x;     fifty%= x;  printf("%d %d %d %d\n", x, hundred, ten, fifty); return0;}

      Output:

      10 100 10 5010 1000 1 0

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.5.16 Assignment operators (p: 72-73)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.5.16 Assignment operators (p: 101-104)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.5.16 Assignment operators (p: 91-93)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.3.16 Assignment 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)

      [edit]See also

      C++ documentation forAssignment operators
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/operator_assignment&oldid=142209"

      [8]ページ先頭

      ©2009-2025 Movatter.jp