what is difference between ++i and i+=1 from any point of view

前端 未结 7 510
一个人的身影
一个人的身影 2021-01-03 06:10

This is a question from kn king\'s c programming : a modern approach. I can\'t understand the solution given by him:-

The expression ++i is equivalent to (i          


        
相关标签:
7条回答
  • 2021-01-03 06:43

    ++i is the pre-increment operator. It increments i before setting and returning the value (which is obviously i + 1).

    Now, i++ is the post-increment operator. It increments i after the whole instruction it appears in is evaluated.

    Example:

    int i = 0;
    std::cout << ++i << std::endl; /* you get 1 here */
    std::cout << i++ << std::endl; /* you still get 1 here */
    std::cout << i << std::endl;   /* you get 2 here */
    
    0 讨论(0)
提交回复
热议问题