C - Prefixing the ++ and -- operators

Introduction

The ++ operator always increments a variable's value, and the -- operator always decrements.

Consider this statement:

a=b++; 

If the value of variable b is 16, you know that its value will be 17 after the ++ operation.

C language math equations are read from left to right.

After the preceding statement executes, the value of variable a is 16, and the value of variable b is 17.

Demo

#include <stdio.h> 

int main() // w ww . jav  a2  s .c o  m
{ 
    int a,b; 

    b=16; 
    printf("Before, a is unassigned and b=%d\n",b); 
    a=b++; 
    printf("After, a=%d and b=%d\n",a,b); 
    return(0); 
}

Result

When you place the ++ or -- operator after a variable, it's called post-incrementing or post-decrementing, respectively.

If you want to increment or decrement the variable before it's used, you place ++ or -- before the variable name; for example:

a=++b; 

In the preceding line, the value of b is incremented, and then it's assigned to variable a.

Related Topic