C decrement Operator Basic C Programming

C Decrement Operator

C programming language also supports another class of arithmetic operators that is decrement operator. As the name suggests this operator is used to decrement the value of any variable by 1. It needs only one operand to be performed. It is indeed called the unary operator. it is symbolically represented by two plus signs connected together (–). It is represented in both prefix and postfix methods. This operator is used to decrement the value of a variable by one still it’s working in both methods of prefix and postfix is slightly different. Those methods will be discussed later part of this tutorial.

Example: –t is same as t = t-1;
Since each and every element in the C programming language has some definite rules and they have to strictly follow that. This decrement operator does the same. It cannot operate on expressions and constants rather works only with the variable.

Prefix decrement Operator

This operator is written just before the variable (–t), Here t is variable, and (–) is the decrement operator. To understand this well, we take the help of an example. Assuming that ‘t’ is a variable and its initial value is 7 and it is written in a program in the following way.

Example: u = –t;
Explanation: The execution of this programming statement would be something like this: First the decrement operator will be executed and decrease the value of ‘t’ by one unit. The new value (6) will be assigned to the variable ‘u’.

Example:
#include<stdio.h>
main()
{
int a=7;
printf(" a = %d", --a);
}
Output: a = 6

Post decrement Operator

This operator is written just after the variable (t–), Here t is variable. To understand its manipulation, we take the help of an example. Assuming that ‘t’ is a variable and its initial value is 7 and it is written in a program in the following way.

Example: u = t–;
Explanation: The execution of this programming statement would be something like this: First the value (7) will be assigned to the variable ‘u’, then there will be a decrement of one unit in ‘t’.

Example:
#include<stdio.h>
main()
{
int a=7;
printf(" a = %d", a--);
}
Output: a = 7

In this tutorial, we have learned about the C decrement operator and both its aspects of prefix decrement operator and postfix decrement operator. In the next tutorial, we will learn about the Relational Operators in C.

Leave a Comment

Your email address will not be published. Required fields are marked *