Home  >  Article  >  Backend Development  >  In C/C++, there are two operations: pre-increment and post-increment.

In C/C++, there are two operations: pre-increment and post-increment.

王林
王林forward
2023-08-25 14:25:171405browse

In C/C++, there are two operations: pre-increment and post-increment.

Here we take a look at what is pre-increment and post-increment in C or C. Both pre-increment and post-increment are increment operators. But there is little difference between them.

Pre-increment operator first increments the value of a variable and then assigns it to other variables, but in case of post-increment operator, it first assigns to a variable variable and then increments the value.

Example

#include<iostream>
using namespace std;
main() {
   int x, y, z;
   x = 10;
   y = 10;
   z = ++x; //z will hold 11
   cout << "Z: " << z << endl;
   z = y++; //z will hold 10, then y will be 11
   cout << "Z: " << z << " and y is: " << y << endl;
}

Output

Z: 11
Z: 10 and y is: 11

Post-increments have a higher priority than pre-increments, and their associativity is also different. Pre-increment associativity is from right to left, post-increment associativity is from left to right.

The above is the detailed content of In C/C++, there are two operations: pre-increment and post-increment.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete