# Pre Increment

Page 1 of 1

## 5 Replies - 966 Views - Last Post: 10 March 2011 - 10:13 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=221200&amp;s=e912ebc1f69fd2fa7c9a8a096173f1a1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shimsha

Reputation: 0
• 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

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

Reputation: 84
• Posts: 1,385
• 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;
}

```

### #3 Salem_c

• void main'ers are DOOMED

Reputation: 2156
• Posts: 4,219
• 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.

### #4 IngeniousHax

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

Reputation: 84
• Posts: 1,385
• 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.

### #5 NickDMax

Reputation: 2255
• 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.

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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.