Pre decrement and Post decrement operator in C programming

10219

As we saw that increment unary operator in C programming is used to increment the value of the variable by 1. Similarly, C language supports one another unary operator known as decrement unary operator whose working behavior is like increment operator but the difference is that it decreases the value of the variable by 1 in place of increment. It is very easy to use and understand C Programming decrement, unary operator. Complete detail about the decrement unary operator of C language is given below with a suitable easy code example.

There are two Unary Operators used in the C Programming language;

  1. Increment and
  2. Decrement

Decrement Unary Operator in C Programming:

An operator that takes the current value of the variable and subtracts one in them and provides the resultant value of concerned variable.

Properties and features of Decrement unary operator:

  • A decrement unary operator is used to decrease the current value of a variable by subtracting integer 1.
  • Like Increment unary operator, the decrement unary operator can be applied to only variables.
  • A decrement operator is denoted by .

Types of decrement operators are used in C programming:

  • Pre-decrement, and
  • Post-decrement

Pre-decrement unary operator

Pre-decrement unary operator is used to decrease the value of a variable by one before using in the expression. In the pre-decrement operator concern value is first decremented and then it is used inside the expression with the final updated value.

Syntax of Pre- decrement unary operator:

  • –variable;

Example Pre-decrement unary operator:

  • –i; //it is equivalent to the i=i-1; or i-=1 ;

Programming Code 1:

#include <stdio.h>
int main()
{
int i=5;
printf(“%d\n”,–i);
return 0;
}

Output: 4

Explanation: In the above example the value of ‘i’ is initialized by 5 after applying the pre-decrement operation on variable ‘i’ then it becomes 4 and the output is printed as 4.

In other words, it is explained that if we push the expression into the stack the value of variable ‘i’ in pre-decrement could not be executed until the expression is completely executed. After complete execution of the expression in the stack the value of variable ‘i’ is decremented by 1 and the output of the expression is 4 because pre decrement unary operator uses the final updated value of the stack i.e. the top value in the stack.

Programming Code 2:

#include <stdio.h>
int main()
{
int i=5;
printf(“%d , %d\n”,–i ,–i);
return 0;
}

Output: 3, 3

Explanation: In the above example the value of ‘i’ is initialized by 5 after that applying the pre-decrement operation on variable ‘i’ then it becomes 4 and once again applying the pre-decrement operator it becomes i=3, the output is printed as 3, 3 (because after complete execution of the expression the value of variable ‘i’ is 3, So both the pre-decrement variable updated at last with the updated value of variable ‘i’).

In other words, it is explained by expression execution in stack operation. We push the expression into the stack the value of variable ‘i’ in pre-decrement and could not be executed until the expression is completely executed. After complete execution of the expression in the stack the value of variable ‘i’ is decremented by 1 twice and the output of the expression is 3, 3 because the pre-decrement unary operator uses the final updated value of the stack i.e. the top value in the stack.

Post-decrement unary operator:

In Post-decrement first of all the loop executes then the value of the concern variable is decremented by 1 and returns the value before the present update state.

Post-increment unary operator is used to decrement the value of a variable as soon as after executing the expression completely in which post decrement is used individually. In the post-decrement, value is first used in an expression and then decremented. In other words, the post-decrement unary operator returns the value before the present state updating.

Syntax of Post-decrement unary operator:

  • Variable–;

Example Post-increment unary operator:

  • –i;

Programming Code 1:

#include <stdio.h>
int main()
{
int i=5;
printf(“%d\n”,i–);
return 0;
}

Output: 5

Explanation: In the above example the value of ‘i’ is initialized by 5 after applying the post-decrement operation on variable ‘i’ then it is updated to 4 but the output is printed before the update i.e. 5.

Programming Code 2:

#include <stdio.h>
int main()
{
int i=5;
printf(“%d , %d\n”,i– , i–);
return 0;
}

Output: 4, 5

Note: The execution of Unary Operator (Post-decrement or Pre-decrement execute from Right to left).

Attempt Free C Programming MCQ Quiz

Previous QuizPhysics Timeline
Next QuizHow to troubleshoot 3 phase Induction motor : Step by Step Guide

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.