Pre-increment (or pre-decrement) in C

CProgrammingServer Side Programming

Increment operators are used to increase the value by one while decrement works opposite increment. Decrement operator decreases the value by one.

Here is the syntax of pre-increment operator in C language,

++variable_name;

Here is the syntax of pre-decrement operator in C language,

--variable_name;

Let us see the difference between pre-increment and pre-decrement operator.

Pre-increment − Before assigning the value to the variable, the value is incremented by one.

Here is an example of pre-increment in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   int i = 5;
   printf("The pre-incremented value : %d\n",i);
   while(++i < 10 )
   printf("%d\t",i);
   return 0;
}

Output

The pre-incremented value : 5
6789

Pre-decrement − Before assigning the value to the variable, the value is decremented by one.

Here is an example of pre-decrement in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
int i = 10;
   printf("The pre-decremented value : %d\n",i);
   while(--i > 5 )
   printf("%d\t",i);
   return 0;
}

Output

The pre-decremented value : 10
9876
raja
Published on 05-Oct-2018 14:03:49
Advertisements