According to syntax postfix increment returns copy of unmodified variable (C++ == C), while prefix increment returns incremented variable (++C == C + 1).

  • Blue_Morpho@lemmy.world
    link
    fedilink
    arrow-up
    7
    arrow-down
    2
    ·
    edit-2
    2 months ago

    ???

    If c = 1, then c++ = 2

    #include <iostream> using namespace std;

    int main() {

    int i = 10;

    cout << i++ << endl;

    cout << i << endl;

    }

    postfix ++ increments the variable.

    • LambdaRXOP
      link
      fedilink
      arrow-up
      6
      ·
      2 months ago

      Postfix increments variable too, but as a side effect. in your code cout << i++ << endl; prints 10 which means, that i++ returned copy of unincremented i.

      • Blue_Morpho@lemmy.world
        link
        fedilink
        arrow-up
        1
        arrow-down
        1
        ·
        2 months ago

        Yes c++ == c. That’s the point Bjarne Stroustrup made. It is the C language but then it’s better.

        Nowadays they’re not completely compatible. But originally it was a preprocessor that created the C equivalent to be compiled. You could write C++ that compiled with a C compiler as long as you didn’t use the extra features.

        • davidagain@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          2 months ago

          Yeah.

          Perhaps ++C == Java or something.

          I’m sure that’s offensive to some, so apologies for airing the thought.