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

There are two Unary Operators used in C Programming language;

  1. Increment and
  2. Decrement

Decrement Unary Operator in C Programming:

An operator that takes current value of variable and subtract one in them and provide resultant value of concern variable.

Properties and features of Increment unary operator:

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

Types of decrement operator 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 variable by one before using in the expression. In the pre-decrement operator concern value is first decremented and then it used inside the expression with final updated value.

Syntax of Pre- decrement unary operator:

  • –variable;

Example Pre- decrement unary operator:

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

Programing Code 1:

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

Output: 4

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

In other words, it is explain as if we push the expression in to the stack the value of variable ‘i’ in pre-decrement could not be execute until the expression completely executed. After completely execution of expression in 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. 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 initialize by 5 after that applying the pre-decrement operation on variable ‘i’ then it becomes 4 and ones again applying 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 updated value of variable ‘i’).

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

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

Post-increment unary operator is used to decrement the value of variable as soon as after executing 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, post-decrement unary operator returns the value before 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 initialize by 5 after that applying the post-decrement operation on variable ‘i’ then it updated 4 but the output is print before 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 form Right to left).

LEAVE A REPLY

Please enter your comment!
Please enter your name here