C++ prefix and postfix
Date: 2023-02-20Last modified: 2024-11-02
Table of contents
Suffix/postfix increment and decrement
a++
,a--
- associativity: left to right
- precedence 2
Prefix increment and decrement
++a
,--a
- associativity: right to left
- precedence 3
int N{5};
fmt::print("N++: {} N: {}\n", N++, N);
N = 5; // reset;
fmt::print("++N: {} N: {}\n", ++N, N);
N = 5; // reset;
fmt::print("N--: {} N: {}\n", N--, N);
N = 5; // reset;
fmt::print("--N: {} N: {}\n", --N, N);
N = 5; // reset;
fmt::print("++--N: {} N: {}\n", ++--N, N);
Possible output
N++: 5 N: 6
++N: 6 N: 6
N--: 5 N: 4
--N: 4 N: 4
++--N: 5 N: 5