Increment/Decrement Operators in C

The increment operator is used to increment ( adds ) the value of operand/variable by 1, whereas the decrement operator is used to decrement ( subtracts ) the value of operand or variable by 1.

Both increment and decrement operators are used on a single operand, therefore it is also called as unary operators.

Unary operator has highest priority compare to other operators.

Both increment and decrement operator divided in to two categories i.e.

Pre-increment: First increment the value of operand or variable, then assigns to it to the variable or operand.

Syntax : ++varName

Post-increment: The value of the variable or operand is incremented after assigning it to the variable or operand.

Syntax : varName++

Pre-decrement: First decrements the value of operand or variable, then assigns to it to the variable or operand.

Syntax : –varName

Post-decrement: The value of the variable or operand is decremented after assigning it to the variable or operand.

Syntax : varName–

Here is the C program to demonstrate the increment operators.


/**
* To demonstrate the increment operators in C
*/
#include<stdio.h>
int main ( int argc, char **argv )
{
int x = 10; /* For Pre-increment */
int y = 12; /* For Post-increment */
/* First increment the value of x
* then assigns it to the variable x
*/
printf("Pre-incerment : ++x = %d \n", ++x );
/* The value of the variable y is incremented
* after assigning it to the variable y
*/
printf("Post-increment : y++ = %d \n", y++ );
return 0;
}

Here is the C program to demonstrate the decrement operators.


/**
* To demonstrate the decrement operators in C
*/
#include<stdio.h>
int main ( int argc, char **argv )
{
int x = 10; /* For Pre-decrement */
int y = 12; /* For Post-decrement */
/* First decrement the value of x
* then assigns it to the variable x
*/
printf("Pre-decrement : --x = %d \n", --x );
/* The value of the variable y is decremented
* after assigning it to the variable y
*/
printf("Post-decrement : y-- = %d \n", y-- );
return 0;
}

Hope, it would be useful !!

To contribute :

” If you like Advance Computing and would like to contribute, you can  mail your article to “computingadvance@gmail.com”. You will get credit as your name , email id, designation with article on this blog. “

Leave a Reply