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

What does i-- mean in c++

下次还敢
下次还敢Original
2024-05-01 14:39:151176browse

The i-- operator in C performs a post-subtraction operation, first assigns the value of i to a temporary variable, subtracts 1 and then updates the value of i. This is different from the prepended subtraction operator (i--), which updates the value of i before subtracting 1.

What does i-- mean in c++

The post-increment and decrement operations represented by i-- in C

i-- in C The operator represents a post-subtraction operation, which performs the following operations:

  • First assign the value of i to a temporary variable.
  • Update the value of i to a temporary value after subtracting 1.

For example:

<code class="cpp">int i = 5;
int j = i--; // j 为 5,i 为 4</code>

In the above example, after assigning the value of i to j, the value of i is decremented by 1. Therefore, the value of j is 5 and the value of i is 4.

The difference between the preceding subtraction operator (i--) and the preceding subtraction operator (i--)

The preceding subtraction operator (i--) will update i before subtracting 1 value, while i-- does the opposite.

For example:

<code class="cpp">int i = 5;
int j = --i; // j 为 4,i 为 4
int k = i--; // k 为 4,i 为 3</code>

In the above example, --i decrements the value of i by 1 before updating the value of i, so the value of j is 4. On the other hand, i-- is decremented by 1 after assigning the value of i to k, so the value of k is 4 and the value of i is 3.

The above is the detailed content of What does i-- 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