Home  >  Article  >  Backend Development  >  What does --a mean in c++

What does --a mean in c++

下次还敢
下次还敢Original
2024-05-09 01:51:15424browse

The --a operator in C is a unary decrement operator, which decrements the value of variable a by 1. Divided into prefix operators and postfix operators, the former uses the value after decrement, and the latter uses the value before decrement.

What does --a mean in c++

The meaning of --a in C

In C, --a is A unary decrement operator that decrements the value of variable a by one.

Usage:

--a can be used as a prefix operator or a postfix operator:

  • Prefix operator: --a First decrement a by 1, and then assign the result to a. For example:
<code class="cpp">int a = 5;
--a; // 结果为 a = 4</code>
  • Postfix operator: a-- Return the value of a first, and then a Decrement by 1. For example:
<code class="cpp">int a = 5;
int b = a--; // 结果为 b = 5, a = 4</code>

Difference:

Prefix decrement and suffix decrement differ in the time used for variables:

  • Prefix decrement: Use the decremented value.
  • Suffix decrement: Use the value before decrement.

Example:

<code class="cpp">int a = 5;

cout << --a << endl; // 输出:4(前缀递减)
cout << a-- << endl; // 输出:4(后缀递减)
cout << a << endl; // 输出:3(后缀递减后)</code>

The above is the detailed content of What does --a mean in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn