C - Operator Decrement Operator

Introduction

The decrement operator has the form -- and subtracts 1 from the variable.

Assuming the variables are of type int, the following three statements all have exactly the same effect:

count = count - 1;
count -= 1;
--count;

They each decrement the variable count by 1.

Suppose count has the value 10 in the following statement:

total = --count + 6;

The variable total will be assigned the value 15 from 9 + 6.

The variable count, with the initial value of 10, has 1 subtracted from it before it is used in the expression so that its value will be 9.

For count--, the decrementing of count occurs after its value has been used.

Suppose count has the value 5 in this statement:

total = --count + 6;

total will be assigned the value 10 (from 4 + 6). In this statement:

total = 6 + count-- ;

total will be assigned the value 11 (from 6 + 5).

Both the increment and decrement operators can only be applied to integer types and character codes.

Related Topic