C Increment Operator Prefix Increment Operator Post Increment Operator

C Increment Operator

In this tutorial, we will learn about the C increment operator. C programming language also supports another class of arithmetic operators that is an increment operator. As the name suggests this operator is used to increment 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 increment the value of a variable by one still it’s working in both methods of prefix and postfix is slightly different. We will learn that next heading.
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 increment operator does the same it cannot operate on expressions and constants rather works only with the variable.

Prefix Increment Operator

This operator is written just before the variable (++t), Here t is variable, and ++ is the increment operator. To understand this well, we take the help of an example. Assuming that ‘t’ is a variable and its initial value is 2 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 increment operator will be executed and increase the value of ‘t’ by one unit. The new value (3) will be assigned to the variable ‘u’.

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

Post Increment Operator

This operator is written just after the variable (t++), Here t is variable. To understand this, we take the help of an example. Assuming that ‘t’ is a variable and its initial value is 2 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 (2) will be assigned to the variable ‘u’, then there will be an increment of one unit in ‘t’.

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

In this tutorial, we have learned about the C increment operator and both its aspects of pre-increment operator and post-increment operator. In the next tutorial, we will learn about the C decrement operator.

Leave a Comment

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