31
Comments
  • 17
    But there is a difference...
  • 1
    @hubiruchi I know, ++i is more efficient because i++ makes copy ... xD
  • 11
    @vukdev not only that. ++i return the value of i after increment, and i++ return the value before the incrememt
  • 0
    @hubiruchi Yes, but there is always a person who will write i++
  • 9
    I always use i++ what you talking about
  • 2
    @hubiruchi I don't think he knew.
  • 3
    Are you suggesting: for (int i = -1; i < x; ++i)?
  • 0
    @devTea ++i will increment the value of i, and then return the incremented value and i++ will increment the value of i, but return the original value
  • 0
  • 4
    Ok this is strange tbh, I mean returns a copy, but that helps for embedded systems where every byte counts
  • 3
    i would be surprised if the compiler wouldn't optimized this and even directly stripped out the return value if not needed.

    To @gitpush: In embedded systems where every byte counts you are gonna use assembly! No need to include all of that libc bloat! (even tho you can probably somehow compile without it)
  • 2
    @gitreflog tbh I'm not an embedded system dev nor I have info about it, but you have a point in what you said also about the part where compiler strips out non needed return value, though imo it is good for people to know the difference till that much and not like ++i adds then return and i++ return then add
  • 2
    I wanted to point out the folliwing:
    ++i != i++

    But then I realized that actually false
  • 2
    @Awlex but i++ != ++i
  • 2
    @gitreflog Embedded compiling without libc is easy - just don't use its functions.

    But assembly is gone in embedded, there is enough memory for C. The dev times are much shorter, that's why.
  • 5
    ++i ftw!
Add Comment