非常教程

C参考手册

C 语法

Increment/decrement operators

递增/递减运算符是一元运算符,用于将变量的值递增/递减1。

他们可以有后缀的形式:

expr ++

expr --

以及前缀形式:

++ expr

-- expr

前缀和后缀增量或减量的操作数 expr 必须是整数类型(包括_Bool和枚举),实际浮点类型或指针类型的可修改左值。它可能是 cvr 限定的,不合格的或原子的。

后缀增量和减量运算符的结果是 expr 的值。

前缀递增运算符的结果是将值添加1到expr的值的结果:表达式++e等同于e+=1。前缀递减运算符的结果是1从 expr 的值中减去该值的结果:该表达式--e等同于e-=1

增量运算符会启动将1适当类型的值添加到操作数的副作用。递减运算符启动1从操作数中减去适当类型值的副作用。与任何其他副作用一样,这些操作在下一个序列点或之前完成。int a = 1; int b = a++; // stores 1+a (which is 2) to a // returns the value of a (which is 1) // After this line, b == 1 and a == 2 a = 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

对任何原子变量进行后增或后减是对存储器顺序 memory_order_seq_cst的原子读取 - 修改 - 写入操作。

(自C11以来)

有关指针运算的限制,请参阅算术运算符以及应用于操作数的隐式转换。

笔记

由于涉及的副作用,必须谨慎使用增量和减量运算符,以避免由于违反排序规则而导致的未定义行为。

递增/递减运算符没有为复数或虚数类型定义:加/减实数1的通常定义对虚数类型没有影响,并且i对于虚数使其增加/减少,但是1对于复数,它会使其处理0+yi不同从yi

与C ++(以及C的某些实现)不同,递增/递减表达式本身并不是左值:&++a无效。

#include <stdlib.h>
#include <stdio.h>
 
int main(void) {
 
  int a = 1;
  int b = 1;
 
  printf("\n");
  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);
 
  // Reset a and b.
  a = 1;
  b = 1;
 
  printf("\n");
  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);
}

输出:

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

参考

  • 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

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.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
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 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。