Increment and Decrement Operators in C

For Complete YouTube Video: Click Here

In this class, we will try to understand Increment and Decrement Operators in C.

In our previous classes, we had our discussion on Arithmetic operators and Assignment operators.

What are Incrementing and Decrementing?

Adding 1 to the value of the variable is called incrementing.

Subtracting the value of a variable by one is called decrementing.

The compound assignment operators a += 1, b -= 1, can do incrementing and decrementing.

C provides a two-shortened operator for incrementing [++] and decrementing [–] the value of a variable by 1.

How to use the Increment and Decrement operator?

We can increment or decrement the variable’s value by using those symbols besides the variable.

For example, a++ will increment the value of a by 1.

Similarly, b– will decrement the value of the variable by 1.

Postfix and Prefix Increment and Decrement operators

As we have the a++ and a– can be used to increment and decrement the variable’s value by 1.

The symbols of increment and decrement operator symbols can be prefixed before the variable name.

For example, ++a, –b is another way to use the increment or decrement operators.

This kind of utilization is called prefix operator.

Similarly, a++, b– is the postfix operator.

Prefix Increment and Decrement Operator

We will try to understand how the prefix operator works with programming examples.

Increment and Decrement operators in C 1
Prefix Increment Operator

The second line of code is the use of the prefix increment operator.

In prefix, the value of i is incremented by 1, and then it returns the value.

The output of the above program is 2 2

Similarly, decrementing is also done.

Postfix Increment and Decrement Operator

The image below shows the use of the postfix increment operator.

Increment and Decrement operators in C 2
Postfix Increment Operator

The second line of code is the use of the postfix increment operator.

In postfix, the original value of i is returned first, and then i is incremented by 1.

The output of the program is 1 2.

Similarly, decrementing is also done.