# 2.8 自增运算符与自减运算符

**Increment and Decrement Operators（递增和递减操作符）**:

* `++` : Increment（递增）
* `--` : Decrement（递减）

**前缀（prefix）**&#x4F7F;用与**后缀（postfix）**&#x4F7F;用，有一些差别。

<pre class="language-c"><code class="lang-c"><strong>++i;
</strong><strong>i++;
</strong></code></pre>

* 前缀，立即自增，其**副作用**是自增 i
* 后缀，先用 i 的原始值，稍后再自增 i

如下是在字符串 s 中删除字符 c

{% code title="squeeze-function.c" lineNumbers="true" %}

```c
void squeeze(char s[], int c)
{
    int i,j;
    for (i=j=0; s[i] != '\0'; i++) 
    {
        if (s[j] != c) 
        {
            s[j++] = s[i];
        }
        s[j] = '\0\;
    }
}
```

{% endcode %}

先拷贝再递增，如下两个写法等价。

```c
if (s[j] != c) 
{
    s[j++] = s[i];
}
```

```c
if (s[j] != c) 
{
    s[j] = s[i];
    j++;
}
```

这里要提到几个术语：

* 由实现定义的行为 implementation-defined，c标准故意对c语言的部分内容未加指定，并认为其细节可以由“实现”来具体定义。
* 未定义行为 undefined behavior，其后果是不可预料的，要尽力避免。
* 副作用 side effect，大多数c语言运算符不会改变操作数的值，但是也有一些会改变。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://labspc.gitbook.io/cnippets/chap2.-lei-xing-yun-suan-fu-yu-biao-da-shi/2.8-zi-zeng-yun-suan-fu-yu-zi-jian-yun-suan-fu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
