Increment and Decrement Operators in C

Introduction

In this post, I am going to explain increment and decrement operators in c programming. Both these operators are unary operators. That means they work on a single operand only.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

We can use these operators in loops. Now, let us see what are increment and decrement operators.

What is Increment and Decrement Operator?

In the c programming, ++ is known as increment operator and — is known as decrement operator.

The increment operator increments the value of an operand by 1. Suppose the value of a=5, then ++a or a++ is equivalent to a = a + 1. After executing this statement, the value of a will be 6.

Also Read: C Program to Print Multiples of 5 using do-while loop

Similarly, the decrement operator decrements the value of an operand by 1. Take the same value of a is 5, then –a or a– is equivalent to a = a – 1. That means the value of a will be 4.

We can use these operators mainly in all types of loops.

It seems very easy to use these operators when we use them independently. But when we use them in an expression then the most logical part starts. These two operators can be prefix and postfix operators.

Also Read: C Program to Remove Zeros from a number

Now, let us see them in detail.

What is Preincrement and Predecrement in C?

When we write ++ or — operator before an operand then it is known as pre-increment or pre-decrement operator respectively. Consider the following code:

int main()
{
   int a = 5, b = 10;
   int x, y;
   x = ++a;
   y = --b;
   printf("a = %d b = %d x =%d y = %d",a,b,x,y);
   return 0;
}

What will be the values of a, b, x and y? Here, the value a is 6, b is 9, x is 6 and y is 9. How?

In pre-increment or pre-decrement, the value of an operand will increment or decrement first and then that value be used or assigned. In this case, the values of a and b are incremented first and then that updated value is assigned to the variable x and y respectively.

Also Read: The while loop in C Programming

As the name suggests pre means first. Therefore pre-increment means increment first and pre-decrement means decrement first.

What is Postincrement and Postdecrement in C?

In the post-increment or post decrement, the value of an operand is incremented or decremented later. Before that, the value of an operand will be used or assigned. Take the same example.

int main()
{
   int a = 5, b = 10;
   int x, y;
   x = a++;
   y = b--;
   printf("a = %d b = %d x =%d y = %d",a,b,x,y);
   return 0;
}

What will be the output of the above program? The answer is quite different. This time the value of a is 6, b is 9, x is 5 and y is 10. Have you found the difference?

Before incrementing the value of a, its original value i.e. 5 is assigned to the variable x and then the value of a is incremented to 6. Similarly, the initial value of b is assigned to y and then the value of b is decremented by 1.

I hope you have understood increment and decrement operators in c programming.

If you still have any difficulty, then you can contact me.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

FAQs

What is difference between ++i and i++ in C?

++i is a pre-increment operator. The value of i will be incremented first and then that updated value will be used in an expression.
i++ is a post-increment operator. The value of i will be used first and then the value of i will be incremented.

Leave a Comment