Back to TILs

C++ prefix and postfix

Date: 2023-02-20Last modified: 2023-02-21

Table of contents

Suffix/postfix increment and decrement

Prefix increment and decrement

  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

References