coperatorspost-increment

Why does integer++ not increment integer value?


Why does num1++ not increment in the printf()?

int num1 = 1;
printf("num1=%d", num1++);

Why does this print num1=1 instead of num1=2?


Solution

  • Because the expression

    num1++
    

    evaluates to num1.


    You may want to do:

    ++num1

    which evaluates to num1 + 1.


    Note however that both expressions increment num1 by one.

    Evaluating num1 in the next statement evalutes to the incremented value.


    In short

    In C, why doesn't num1++ increment in the printf()?

    num1++ does increment num1 but it evaluates to num1 and that evaluation is what you are passing to printf().