Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What is the difference between ++i and i++?

In C language, what is the difference between using
++i and i++,
and which should be used in the increment block of a for loop in C language?
by

3 Answers

hiten349
++i is a pre-Increment and i++ is a post-Increment that means ,In a ++i value is incremented first and in i++ value is incremented after a first execution
Sonali7
The major difference between i++ and ++i is that i++ returns the value before it incremented whereas ++i returns the value after it is incremented. In simple terms, i++ returns the original value of i, and then it increments whereas ++i first increments the value and then returns that incremented value.
sandhya6gczb
++i and i++ both are unary operators used for incrementing value by one. ++i is called a pre-increment operator as it first increments the value and then it assigns to variable whereas i++ is a post-increment operator which first assigns the value to the variable and then it increments the value.

#include<stdio.h>
int main()
{
int i ,j ,x,z;
i=10;
j=10;
x=++i; //pre-increment
z=j++; //post increment
printf("the result = %d ",x);
printf("the result = %d ",z);
return 0;
}

Run this program and you will find that x has been assigned incremented value whereas z has been assigned the original value.
For loop, pre-increment can be preferred as it takes less time for internal computation.
But in case if you are using the previous value then use the post-increment operator.

Login / Signup to Answer the Question.