非常教程

C参考手册

C 语法

Assignment operators

赋值和复合赋值运算符是二元运算符,它们使用右侧的值将变量修改为左侧。

Operator

Operator name

Example

Description

Equivalent of

=

basic assignment

a = b

a becomes equal to b

N/A

+=

addition assignment

a += b

a becomes equal to the addition of a and b

a = a + b

-=

subtraction assignment

a -= b

a becomes equal to the subtraction of b from a

a = a - b

*=

multiplication assignment

a *= b

a becomes equal to the product of a and b

a = a * b

/=

division assignment

a /= b

a becomes equal to the division of a by b

a = a / b

%=

modulo assignment

a %= b

a becomes equal to the remainder of a divided by b

a = a % b

&=

bitwise AND assignment

a &= b

a becomes equal to the bitwise AND of a and b

a = a & b

|=

bitwise OR assignment

a |= b

a becomes equal to the bitwise OR of a and b

a = a | b

^=

bitwise XOR assignment

a ^= b

a becomes equal to the bitwise XOR of a and b

a = a ^ b

<<=

bitwise left shift assignment

a <<= b

a becomes equal to a left shifted by b

a = a << b

=

bitwise right shift assignment

a >>= b

a becomes equal to a right shifted by b

a = a >> b

简单的任务

简单赋值运算符表达式具有这种形式。

lhs = rhs

其中

lhs

-

modifiable lvalue expression of any complete object type

rhs

-

expression of any type implicitly convertible to lhs or compatible with lhs

赋值执行从 rhs 值到 rhs 类型的隐式转换,然后用转换后的 rhs 值替换由 lhs 指定的对象中的值。

赋值也返回与存储的值相同的值lhs(以便可能的表达式a = b = c)。赋值运算符的值类别是非左值(因此表达式(a=b)=c无效)。

rhs 和 lhs 必须满足以下条件之一:

  • lhs 和 rhs 都具有兼容的结构或联合类型,或..
  • rhs 必须可以隐式转换为 lhs,这意味着
    • lhs 和 rhs 都有算术类型,在这种情况下lhs可能是挥发性限定的或原子的
    • lhs和rhs都有指向兼容(忽略限定符)类型的指针,或者其中一个指针是指向void的指针,并且转换不会为指向类型添加限定符。lhs可能是挥发性的或限制性的或原子的。
    • lhs是一个指针(可能是限定的或原子的),rhs是一个空指针常量,如 NULL
    • lhs有类型_Bool(可能是合格的或原子的),rhs是一个指针

注意

如果 rhs 和 lhs 在内存中重叠(例如它们是同一联合的成员),则行为不确定,除非重叠是准确的并且类型是兼容的。

虽然数组不可分配,但是包装在结构中的数组可以分配给具有相同(或兼容)结构类型的另一个对象。

更新 lhs 的副作用在值计算之后进行排序,但不是 lhs和 rhs 本身的副作用以及操作数的评估像往常一样相对于彼此不相关(所以诸如i=++i; 的表达式是未定义的)。

赋值从浮点表达式中剥离了额外的范围和精度(请参阅参考资料FLT_EVAL_METHOD)。

在 C ++中,赋值运算符是左值表达式,而不是 C.

// todo more, demo struct{array} too
const char **cpp;
char *p;
const char c = 'A';
 
cpp = &p; // Error: char ** is not convertible to const char **
*cpp = &c; // OK, char* is convertible to const char*
*p = 0; // OK, null pointer constant is convertible to any pointer

复合分配

复合赋值运算符表达式具有这种形式。

lhs op rhs

其中

操作

-

* =,/ =%=,+ = - =,<< =,>> =,&=,^ =,| =中的一个

lhs,rhs

-

具有算术类型的表达式(其中lhs可以是限定的或原子的),除了当op是+ =或 - =时,它们也接受与+和 - 相同限制的指针类型,

表达式 lhs @ = rhs 与 lhs =lhs @ (rhs 完全相同),只是 lhs 只被计算一次。

如果 lhs 具有原子类型,则该操作表现为具有内存顺序 memory_order_seq_cst的单个原子读取 - 修改 - 写入操作。对于整数原子类型,复合赋值@ =相当于:T1 * addr =&lhs; T2 val = rhs; T1 old = * addr; T1新; {new = old @ val} while(!atomic_compare_exchange_strong(addr,&old,new);

(自C11以来)

参考

  • C11标准(ISO / IEC 9899:2011):
    • 6.5.16作业操作员(p:101-104)
  • C99标准(ISO / IEC 9899:1999):
    • 6.5.16作业操作员(p:91-93)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 3.3.16赋值运算符

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20. types
21.Basic concepts
22.Bit fields
23.break statement
24.C language
25.C Operator Precedence
26.cast operator
27.character constant
28.Comments
29.Comparison operators
30.compound literals
31.Conditional inclusion
32.Conformance
33.const type qualifier
34.Constant expressions
35.continue statement
36.Declarations
37.do-while loop
38.Enumerations
39.Escape sequences
40.Expressions
41.External and tentative definitions
42.File scope
43.floating constant
44.for loop
45.Function declarations
46.Function definitions
47.Functions
48.Generic selection
49.goto statement
50.Identifier
51.if statement
52.Implicit conversions
53.Increment/decrement operators
54.Initialization
55.inline function specifier
56.integer constant
57.Lifetime
58.Logical operators
59.Lookup and name spaces
60.Main function
61.Member access operators
62.Memory model
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。