top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between ++i, i++ in C/C++?

+1 vote
320 views
What is difference between ++i, i++ in C/C++?
posted Aug 18, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
++i means pre-increment
EX if i=0;
++i;      O/P will b 1
++i        O/P will b 2
like that

and i++ for post increment
EX if i=0;
i++ O/P will b 0
i++ O/P will b 1
like that

2 Answers

0 votes
  1. i++ refers to post increment. It assigns the value first and then increments.
    eg.- int x, i=10;
    x = i++;
    printf("x= %d i = %d\n",x,i);

The result will be x = 10 & i =11;

  1. ++i refers to pre increment. First it increments and then assigns the value.
    eg.- int x, i=10;
    x = ++i;
    printf("x= %d i = %d\n",x,i);

The result will be x = 11 & i =11;

Hope this helps!

answer Aug 18, 2014 by Ankush Surelia
0 votes

The meaning is same for both c and c++.
++i means pre-increment and
i++ means post-increment

For better understanding see the example.

int main()
{
int x, y;
int i = 0;

x = ++i; // here pre-increment the value of i and then store it into x i.e (x = 1)

y = i++; // here post-increment the value of i after storing it into y, i.e (y = 1)
// to verify the above
printf("x = %d and y = %d\n", x, y);

return 0;
}

output :
x = 1 and y = 1

answer Aug 18, 2014 by Arshad Khan
...