Home > Backend Development > C++ > body text

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

王林
Release: 2023-08-25 14:25:17
forward
1372 people have browsed it

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;
}
Copy after login

Output

Z: 11
Z: 10 and y is: 11
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!