5 Replies - 555 Views - Last Post: 10 March 2011 - 10:13 AM Rate Topic: -----

#1 shimsha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-December 10

Pre Increment

Posted 09 March 2011 - 11:32 PM

Hi,

int main()
{
   int i=5,j=5,y,x;
   x=++i + ++i + ++i;
   y=++j + ++j + ++j;
   printf("%d  %d\n",x,y);
   return 0;
}





In above the program i guessed o/p is 24 and 24.But am getting 22 and 22.Please help me to understand the concept of Pre Increment.
Is This A Good Question/Topic? 0
  • +

Replies To: Pre Increment

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Pre Increment

Posted 09 March 2011 - 11:48 PM

I dont wanna try your code, but try this and see if you understand it better.

#include <iostream>

using namespace std;

int main()
{
  int count = 0;
  int MAX = 20;

  for(int i = 0; i < MAX; i++)
  {
    count++;
    cout << "i is [" << i << "] and count is [" << count << "] using a postfix increment op." << endl;
  }
  cout << "Clearing Count" << endl;
  count = 0;
  cout << "Count is now: " << count << endl;
  for(int i = 0; i < MAX; ++i)
  {
    ++count;
    cout << " i is [" << i << "] and count is [" << count << "] using a prefix increment op." << endl;
  }
 return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1675
  • View blog
  • Posts: 3,171
  • Joined: 30-May 10

Re: Pre Increment

Posted 10 March 2011 - 02:01 AM

Read this and learn that whoever is telling you there is an answer to your problem is basically ignorant and clueless.

There is NO answer, because the program exhibits undefined behaviour.
Was This Post Helpful? 2
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Pre Increment

Posted 10 March 2011 - 09:21 AM

Interesting read, I followed then link to lang.c.com or something along those lines and read up on some of the information, and that is stuff i have never heard before... I was always told to use increment op's or decrement ops for shorthand to modify the value of a counter...

My whole life is a lie. :boat:
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Pre Increment

Posted 10 March 2011 - 10:02 AM

shimsha -- it is undefined behavior to use post/pre increment on the same value within the same expression multiple times. So the output of your program is dependent upon the compiler and compiler settings.

@IngeniousHax -- not a lie. You just have to know and follow the rules. Be careful when dealing with side-effects. Using inc/dec for counters/iterators is ok, but don't go about trying to get all fancy and pack everything into one expression.
Was This Post Helpful? 1
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Pre Increment

Posted 10 March 2011 - 10:13 AM

shimsha, the "teaching" of this sort of thing seems to be endemic to your country (along with Turbo C/C++, fflush(stdin), and gets()). I would LOVE to know, in this particular case, if the lesson to be learned here is NOT to do this, or if they are teaching you that it is perfectly normal and acceptable. I suspect the latter, and that seriously makes me angry.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1