Increament Operator
C

Example of Increment or Decrement Operator in C Language

Increment and decrement operators in C

C language contains two unary operators referred to as increment (++) and decrement (–) operators.

The two unary arithmetic operators in C
Increment operator (++)
Decrement operator (- -)

The increment operator increments the variable by one and the decrement operator decrements the variable by one. These operators can be written in two forms i.e. before a variable or after a variable.

These operators can be written in two forms i.e. before a variable or after a variable.

If an increment/decrement operator is written before a variable, it is referred to as pre-increment/pre-decrement operators and if it is written after a variable, it is referred to as post-increment/post-decrement operator.

For example,

a++ or ++a is equivalent to a = a+1 and
a– or – -a is equivalent to a = a -1

The importance of pre and post operator occurs while they are used in the expressions.

Pre-incrementing (Predecrementing) a variable causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears.

Post-incrementing (post decrementing) the variable causes the current value of the variable is used in the expression in which it appears, then the variable value is incremented (decrement) by 1.

Here are some examples of Increment and Decrement operator in the C language

Example

#include<stdio.h>
void main()
{
int a=8, b=2, x=8, y=2, sum1, sum2;
/* Increment then add */
sum1=a+(++b);
/* Add then increment */
sum2=x+(y++);
printf("\nSUM1=%d",sum1); 
printf("\nSUM2=%d",sum2); 
}


Run
SUM1=11
SUM2=10

Using Incrementer

/* using incrementer */
#include<stdio.h>
void main()
{
int i,j;
int x,y;
j=i=6;
printf("initial value of i = %d\n",j);
x=i++;
printf("x=i++, value of x=%d, and value of i=%d\n",x,i); //post increment
i=j;
y=++i;
printf("initial value i = %d\n",j);
printf("y=++i, value of y=%d, and value of i=%d\n",y,i); // pre increment
}

RUN
initial value i=6
x=i++, value of x=6, and value of i=7
initial value i=6
y=++i, value of y=7, and value of i=7

Using Decrementer

/* using decrementer */
#include<stdio.h>
void main()
{
int i,j;
int x,y;
j = i=6;
printf(" initial value i = %d\n", j);
x = i--;
printf("x= i--, value of x = %d, and value of i = %d\n", x,i);
i = j;
y = --i;
printf("initial value i = %d\n", j);
printf("y = --i, value of = %d, and value of i = %d\n", y,i);
}

RUN
initial value i=6
x=i, value of x=6, and value of i=5
initial value  i=6
y=i, value of y=5, and value of i=5

Using Increment and Decrement Operations

/* Increment and decrement operations */
#include<stdio.h>
void main()
{
int a,b,c;
a = b = c = 0 ;
printf( " Initial value of a,b,c %d %d %d \n", a,b,c);
a = ++b + ++c;
printf( " a = ++b + ++c =%d %d %d\n",a,b,c);
a =b++ + c++;
printf(" a =b++ + c++ =%d %d %d\n",a,b,c);
a = ++b + c++;
printf(" a = ++b +c++ =%d %d %d\n",a,b,c);
a =b-- + c--;
printf(" a =b-- + c-- =%d %d %d\n",a,b,c);
}

RUN
Initial value of a,b,c 0 0 0
a = ++b + ++C = 2 1 1
a = b++ + C++ = 2 2 2
a = ++b + C++ = 5 3 3
a = b-- + C-- = 6 2 2

Leave a Reply

Your email address will not be published.

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