Increment & Decrement Operators in C language

Increment and decrement operators in C or other languages like java, .net, PHP are similar. If you understand the basic concept of increment and decrement operators than you can use it in any programming language.

Increment operator in C

The increment operator is used to adding (increase) one value by its previous value. It is denoted by ++ symbol and you can use it with a single operand like I++ or ++I.

Increment operator are two types-

  • Pre-increment operator
  • Post-increment operator

Pre increment operator

You can use pre-increment operator by just placing ++ symbol before any operand like ++j or ++k etc.

Increment and decrement operators in C

It can be used like the below example:

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j;
i=5;
printf("%d", i);

j=++i; //What will be the output of j

printf("%d",j);
getch();
}

Pre-increment is used to first increment then process the statement. It means, when above code statement

j=++i; will execute, it will first increase the value of I by 1 then transfer it to J.

So, the output will be 6.

Post increment operator

You can use post-increment operator by just placing ++ symbol after any operand like m++ or n++ etc.

It can be used like the below example:

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j;
i=5;
printf("%d", i);

j=i++; //What will be the output of j

printf("%d",j);
getch();
}

Post increment is used to the first process the statement then increment the statement. It means, when above code statement

j=i++; will execute, it will first transfer the value of I in J then increase it by 1.

So, the output will be 5.

Increment and decrement operators in C

Decrement operator in C

Decrement operator is used to subtracting (decrease) one value by its previous value. It is denoted by — the symbol and you can use it with a single operand like I– or –I.

We can use a decrement operator by-

  • Pre-decrement
  • Post-decrement

Pre-decrement operator

You can use pre-decrement operator by just placing — symbol before any operand like –j or –k etc.

It can be used like the below example:

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j;
i=5;
printf("%d", i);

j=--i; //What will be the output of j

printf("%d",j);
getch();
}

Pre-decrement is used to first decrement then process the statement. It means, when above code statement j=–i; will execute, it will first decrease the value of I by 1 then transfer it to J.

So, the output will be 4.

Post-decrement operator

You can use post-decrement operator by just placing — symbol after any operand like j– or k– etc.

It can be used like the below example:

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j;
i=5;
printf("%d", i);

j=i--; //What will be the output of j

printf("%d",j);
getch();
}

post-decrement is used to the first process the statement then decrement. It means, when above code statement j=i–; will execute, it will first transfer it to J then decrease the value of I by 1.

So the value of j would be 5.

You can also read

Hope that you understand the concept of increment and decrement operators in C. Please ask your questions in below comment section and don’t forget to share this post on your social media family.

Leave a Comment

error: Don\'t make piracy - Write your own content :)