i need the difference between i++ and ++i, where is i++ used and where ++i is used

Recommended Answers

All 6 Replies

Mmm, a nice looking exam question.
What do YOU know about the difference(s) already.

Show us that you've done a bit of work yourself, and that you might learn something (as opposed to simply dumping your homework, then hoping to copy/pasting the answer).

Eg.
Which would you use in a for loop to increment the loop control variable? for ( i = 0 ; i < 10 ; /*i++ or ++i here? */ ) Which would you use for copying a block of memory?

if you not confident enough then what you have to do is:

you remove the optmization of you're compiler and diassemble you're
binary program and see , then you'll get more confident.

Just use a olydebug like program or on visual C++ IDE there is a diassembly viewer that was enabled on debug time. Go there and read
the code for more self confident.

IMO, it's better to understand the concept from a language point of view, than knowing what ONE single implementation does in ONE specific test case.

Which one is most likely , ++i or i++
:

//++i  OR i++ ?
{
    int temp = i;
    i = i+1;
    return temp;
}
//++i OR i++ ?
{  
    i = i + 1;
    return i;
}

Maybe the name will help you :

++i is called pre-increment
i++ is called post-increment

Answers like:

Which is equivalent to n = n + 1;

  1. ++n;
  2. n++;

are meaningless. They are the same.

A better answer is:

What is the difference between:

  • x = n++;
  • x = ++n;

Once that is understood then you can get into stuff like avoiding object temporaries for non-native types. (See the C++FAQ-Lite.)

Hope this helps.

i need the difference between i++ and ++i, where is i++ used and where ++i is used

++i means increment the value of i by 1 n then use it. "change the value n then use"
i++ means use the value of i as it is and after using increment the value by 1"use and then change"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.