# 自增与自减运算符

自增与自减运算符是一元运算符，写作 `++` 和 `--`，使变量加 1 或减 1。二者各有前缀形式（`++x`、`--x`），取变化后的新值；以及后缀形式（`x++`、`x--`），取操作数变化前的值。它们起源于 B 语言，经由 C 传入 C++、Java、[JavaScript](https://www.edgepedia.org/zh/javascript)、C# 和 PHP，而 Python、Rust、Swift、Zig 等语言则有意将其排除在外。<sup>[1](https://en.cppreference.com/c/language/operator_incdec)</sup><sup> • </sup><sup>[2](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html)</sup><sup> • </sup><sup>[3](https://emersonbraun.github.io/language-comparison/docs/operators/)</sup>

| 关键事实 | 细节 |
|---|---|
| 形式 | 前缀 `++x` / `--x` 与后缀 `x++` / `x--`；前缀等价于 `x += 1`，返回更新后的值；后缀返回原值<sup>[1](https://en.cppreference.com/c/language/operator_incdec)</sup> |
| C 的操作数 | 必须是整型（含 `_Bool` 与枚举）、实浮点型或指针型的可修改左值<sup>[1](https://en.cppreference.com/c/language/operator_incdec)</sup> |
| 指针算术 | 指针操作数按其所指对象的大小前进，因此自增后的指针指向下一个对象<sup>[4](https://learn.microsoft.com/en-gb/cpp/cpp/prefix-increment-and-decrement-operators-increment-and-decrement?view=msvc-170)</sup> |
| 起源 | 由肯·汤普森（Ken Thompson）为 B 语言发明；很可能是 PDP-7 的自增内存单元启发了这一想法，而非 PDP-11——当时它还不存在<sup>[2](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html)</sup> |
| C++ 重载 | 四种形式均可重载；前缀返回 `T&`，后缀接受一个哑元 `int` 参数并返回 `T`<sup>[5](https://en.cppreference.com/cpp/language/operator_incdec)</sup> |
| Go 的变体 | `++` 与 `--` 是语句而非表达式，语义上等价于 `x += 1` 和 `x -= 1`<sup>[6](https://go.dev/ref/spec)</sup> |
| 未采用的语言 | Rust、Python、Swift、Zig 不设这两个运算符；C、C++、C#、Java、JavaScript、PHP 保留<sup>[3](https://emersonbraun.github.io/language-comparison/docs/operators/)</sup> |

## 语义

两种形式都修改操作数，区别在于表达式本身求值得到什么。在 C 中，后缀自增或自减的结果是操作数修改前的值，而前缀形式等价于 `x += 1` 或 `x -= 1`，产生更新后的值。存储新值的副作用在下一个序列点处或之前完成。<sup>[1](https://en.cppreference.com/c/language/operator_incdec)</sup> C 参考手册中的一个简短例子：执行 `int a = 1; int b = a++;` 之后，`b` 为 1，`a` 为 2；而执行 `a = 1; int c = ++a;` 之后，`c` 和 `a` 都是 2。<sup>[1](https://en.cppreference.com/c/language/operator_incdec)</sup>

操作数必须是可赋值的东西。在 C 中，这指的是整型（含 `_Bool` 与枚举）、实浮点型或指针型的可修改左值。<sup>[1](https://en.cppreference.com/c/language/operator_incdec)</sup> 对单纯的数字使用该运算符是错误的，正如 MDN 就 JavaScript 所指出的：`3++;` 会失败，因为只有已存在的变量才能自增。<sup>[7](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Math)</sup>

指针操作数遵循该语言的指针算术规则。在 C++ 中，整型或浮点型的操作数按整数值 1 改变，而指针操作数按其所指对象的大小改变，因此自增后的指针指向下一个对象，自减后的指针指向上一个对象。<sup>[4](https://learn.microsoft.com/en-gb/cpp/cpp/prefix-increment-and-decrement-operators-increment-and-decrement?view=msvc-170)</sup>

某些类型限制随时间推移而收紧。在 C++ 中，对 `bool` 操作数自增曾被弃用，且会将其置为 `true`，直到 [C++17](https://www.edgepedia.org/zh/c-17) 才成为非良构；自 C++20 起，对 volatile 限定类型的操作数自增或自减被弃用。<sup>[5](https://en.cppreference.com/cpp/language/operator_incdec)</sup>

## 在 B 与 C 中的起源

这两个运算符由[肯·汤普森](https://www.edgepedia.org/zh/ken-thompson)在[贝尔实验室](https://www.edgepedia.org/zh/bell-labs)为 B 语言发明。[丹尼斯·里奇](https://www.edgepedia.org/zh/dennis-ritchie)（Dennis Ritchie）由 B 发展出 C，并在 1993 年的一篇论文中记述了这段历史：他记载道，`++` 与 `--` 处于前缀还是后缀位置，决定了改变发生在记录操作数值之前还是之后；这两个运算符在 B 最早的版本中并不存在，是后来才出现的；而将它们推广为既可前缀又可后缀，则是汤普森本人的主意。<sup>[2](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html)</sup>

一种常见的猜测把这两个运算符归因于 DEC PDP-11 的自增与自减寻址模式——C 和 Unix 最初正是在这台机器上流行起来的。里奇称这在历史上不可能，因为 B 语言开发时还没有 PDP-11。PDP-7 确实有几个自增内存单元，很可能正是它们启发了汤普森，尽管这些单元并未直接用于运算符的实现；更强的动机是他观察到 `++x` 的翻译结果比 `x = x + 1` 更短小。<sup>[2](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html)</sup>

## C++ 中的重载

C++ 允许用户自定义类型重载全部四种形式。惯用的原型是前缀 `T& T::operator++()`，后缀 `T T::operator++(int)`，其中 `int` 参数只用于区分两个版本；正常调用后缀运算符时，该参数的值总是 0。对于内建运算符，前缀形式产生引用，后缀形式产生值，用户定义的重载通常模仿这一惯例。<sup>[5](https://en.cppreference.com/cpp/language/operator_incdec)</sup>

由于后缀自增和后缀自减过程中会构造对象的临时副本，在返回值不被使用的场合，前缀运算符通常更高效。<sup>[5](https://en.cppreference.com/cpp/language/operator_incdec)</sup> 编码规范得出了同样的实用结论：后缀自增必须复制并保存一个值用于返回，如果该值未被使用，编译器未必能把这个副本优化掉，因此指南建议对迭代器及其他非标量类型使用前缀自增，而对简单标量值则两种形式皆可。<sup>[8](https://websites.umich.edu/~eecs381/handouts/C%2B%2B_Coding_Standards.pdf)</sup><sup> • </sup><sup>[9](https://usermanual.wiki/Document/Google20C2B2B20Style20Guide.875282820.pdf)</sup>

## 未定义行为与求值顺序

这两个运算符带有副作用，若在同一表达式中与同一变量的其他使用混在一起，可能使程序行为未定义。C 与 C++ 标准规定，在两个序列点之间，对象的存储值至多只能被表达式的求值修改一次；违反此规则的程序在任何具体实现上的结果都完全不可预测。<sup>[10](https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc.pdf)</sup> cppreference 的 C++ 页面警告说，内建的自增与自减运算符必须谨慎使用，以免因违反求值顺序规则而产生未定义行为，其示例将 `n1 + ++n1` 标为未定义行为。<sup>[5](https://en.cppreference.com/cpp/language/operator_incdec)</sup>

在 C 中，对原子变量做后缀自增或后缀自减，被定义为以 `memory_order_seq_cst` 内存序进行的原子读-改-写操作，此规则自 C11 起生效。<sup>[1](https://en.cppreference.com/c/language/operator_incdec)</sup>

## 各语言之间

C 家族的后继语言基本以相同的语义保留了这两个运算符。[ECMAScript](https://www.edgepedia.org/zh/ecmascript) 规范定义了四种更新表达式——后缀与前缀的自增和自减——并规定后缀返回旧值、前缀返回新值，算术运算作用于 Number 或 BigInt 值。<sup>[11](https://tc39.es/ecma262/2024/multipage/ecmascript-language-expressions.html)</sup> Java 语言规范将前缀自增和自减列入其一元运算符。<sup>[12](https://docs.oracle.com/javase/specs/jls/se25/html/)</sup>

有两种语言对这一想法做了改动。Go 只把 `++` 和 `--` 作为语句而非表达式：`x++` 在语义上等价于 `x += 1`，该语句不产生值；又因为它们不在运算符层级之内，`*p++` 的含义是 `(*p)++`。<sup>[6](https://go.dev/ref/spec)</sup> Rust、Python、Swift 和 Zig 则完全不要这两个运算符，宁愿用显式的 `x += 1` 以求清晰，并避免前缀与后缀的混淆。<sup>[3](https://emersonbraun.github.io/language-comparison/docs/operators/)</sup> Rust 论坛上的一次讨论给出了省略它们的两个理由：`++` 的主要动因——C 风格的 `for (init; condition; increment)` 循环——在 Rust 中并不存在，Rust 改用基于迭代器的 `for` 循环；而 `++` 作为表达式所涉及的求值顺序规则，即便对经验丰富的开发者也并非显而易见。<sup>[13](https://users.rust-lang.org/t/solved-with-two-amazing-answers-how-to-increment-in-rust/52452)</sup>

## 可读性

当这两个运算符混入较大的表达式时，其简洁是有代价的，这一点已有研究证实。2003 年发表于《IEEE 软件工程汇刊》的一项交叉设计研究，由巴斯克大学的何塞·哈维尔·多拉多（Jose Javier Dolado）、伦敦大学学院的马克·哈曼（Mark Harman）等人完成，他们用含有自增与自减运算符的 C 代码片段与无副作用的对应片段做对照测试，测量正确率和所花时间。带副作用的运算符显著降低了理解类任务的表现，作者称这为“副作用有害”这一信念提供了实证依据。<sup>[14](https://www.researchgate.net/publication/3188347_An_empirical_investigation_of_the_influence_of_a_type_of_side_effects_on_program_comprehension)</sup> 与此一致，静态分析指南把表达式中 `++` 或 `--` 与其他运算符混用、或作为函数实参传入的情况标记为可读性问题。<sup>[15](https://doc.casthighlight.com/alt_incdec-increment-decrement-operators-not-used-method-call-mixed-operators-expression/)</sup> 在现代惯用法中，这两个运算符大多以独立语句出现，尤其是作为 `for` 循环的更新子句，在那里前缀与后缀之别对程序结果毫无影响。

## References

1. [Increment/decrement operators, C language, cppreference.com](https://en.cppreference.com/c/language/operator_incdec)
2. [Dennis M. Ritchie, "The Development of the C Language" (1993), Bell Labs](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/chist.html)
3. [Operators, Language Comparison](https://emersonbraun.github.io/language-comparison/docs/operators/)
4. [Prefix Increment and Decrement Operators: ++ and --, Microsoft Learn](https://learn.microsoft.com/en-gb/cpp/cpp/prefix-increment-and-decrement-operators-increment-and-decrement?view=msvc-170)
5. [Increment/decrement operators, C++ language, cppreference.com](https://en.cppreference.com/cpp/language/operator_incdec)
6. [The Go Programming Language Specification, IncDec statements](https://go.dev/ref/spec)
7. [Basic math in JavaScript, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Math)
8. [C++ Coding Standards, University of Michigan EECS 381](https://websites.umich.edu/~eecs381/handouts/C%2B%2B_Coding_Standards.pdf)
9. [Google C++ Style Guide](https://usermanual.wiki/Document/Google20C2B2B20Style20Guide.875282820.pdf)
10. [Using the GNU Compiler Collection (GCC) 11.2, sequence point rules](https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc.pdf)
11. [ECMAScript 2024 Language Specification, section 13.4 Update Expressions, TC39](https://tc39.es/ecma262/2024/multipage/ecmascript-language-expressions.html)
12. [The Java Language Specification, Java SE 25, chapter 15 Expressions](https://docs.oracle.com/javase/specs/jls/se25/html/)
13. [How to increment in Rust, Rust Users Forum](https://users.rust-lang.org/t/solved-with-two-amazing-answers-how-to-increment-in-rust/52452)
14. [Dolado, Harman, Otero, and Hu (2003), "An empirical investigation of the influence of a type of side effects on program comprehension," IEEE Transactions on Software Engineering 29(7)](https://www.researchgate.net/publication/3188347_An_empirical_investigation_of_the_influence_of_a_type_of_side_effects_on_program_comprehension)
15. [Increment and decrement operators should not be used in a method call or mixed with other operators, CAST Highlight](https://doc.casthighlight.com/alt_incdec-increment-decrement-operators-not-used-method-call-mixed-operators-expression/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › 编程语言 › 编程语言概念

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/increment-and-decrement-operators

English original: https://www.edgepedia.org/increment-and-decrement-operators
