# preincrement& post increment

Page 1 of 1

## 4 Replies - 3746 Views - Last Post: 07 February 2008 - 07:08 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=42661&amp;s=8fe76a8491929379829fe42bd9366ab1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 selloorhari

Reputation: 5
• Posts: 68
• Joined: 07-February 08

# preincrement& post increment

Posted 07 February 2008 - 03:26 AM

What is the out put of the following program ?

main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("\nPreincremt = %d", ;
a = 5 ;
b = a++ + a++ +a++ ;
printf ( "\n Post increment = %d",b ) ;
}

This post has been edited by selloorhari: 07 February 2008 - 04:21 AM

Is This A Good Question/Topic? 0

## Replies To: preincrement& post increment

### #2 shreshth91

Reputation: 0
• Posts: 4
• Joined: 07-February 08

## Re: preincrement& post increment

Posted 07 February 2008 - 03:35 AM

selloorhari, on 7 Feb, 2008 - 03:26 AM, said:

What is the out put of the following program ?

main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("%d", ;
a = 5 ;
b = a++ + a++ +a++ ;
printf ( "%d",b ) ;
}

First one has pre-increments, so it's equivalent to 6+7+8 = 21
And the second will be 5+6+7 = 18 since it has post-increments

### #3 selloorhari

Reputation: 5
• Posts: 68
• Joined: 07-February 08

## Re: preincrement& post increment

Posted 07 February 2008 - 04:22 AM

shreshth91, on 7 Feb, 2008 - 03:35 AM, said:

selloorhari, on 7 Feb, 2008 - 03:26 AM, said:

What is the out put of the following program ?

main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("%d", ;
a = 5 ;
b = a++ + a++ +a++ ;
printf ( "%d",b ) ;
}

First one has pre-increments, so it's equivalent to 6+7+8 = 21
And the second will be 5+6+7 = 18 since it has post-increments

But i got 24 as the first O/P
and 15 as the second OP
How is it?

### #4 shreshth91

Reputation: 0
• Posts: 4
• Joined: 07-February 08

## Re: preincrement& post increment

Posted 07 February 2008 - 06:11 AM

selloorhari, on 7 Feb, 2008 - 04:22 AM, said:

shreshth91, on 7 Feb, 2008 - 03:35 AM, said:

selloorhari, on 7 Feb, 2008 - 03:26 AM, said:

What is the out put of the following program ?

main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("%d", ;
a = 5 ;
b = a++ + a++ +a++ ;
printf ( "%d",b ) ;
}

First one has pre-increments, so it's equivalent to 6+7+8 = 21
And the second will be 5+6+7 = 18 since it has post-increments

But i got 24 as the first O/P
and 15 as the second OP
How is it?

That must mean that your compiler is first incrementing all the values and then displaying in the pre-increment one (8+8+8=24)
And in the post-increment one, it's first displaying and then incrementing all the values (5+5+5=15, final value of a is 8)

This must differ from compiler to compiler. Turbo-C++ shows 21 and 18, and Bloodshed Dev-C++ shows 22 and 15 (22 because of some complications arising due to multiple '+' signs being taken as multiple increment operators; fixed that using a loop to increment).
Just out of curiosity, what IDE were you using?

This post has been edited by shreshth91: 07 February 2008 - 06:34 AM

### #5 selloorhari

Reputation: 5
• Posts: 68
• Joined: 07-February 08

## Re: preincrement& post increment

Posted 07 February 2008 - 07:08 AM

shreshth91, on 7 Feb, 2008 - 06:11 AM, said:

selloorhari, on 7 Feb, 2008 - 04:22 AM, said:

shreshth91, on 7 Feb, 2008 - 03:35 AM, said:

selloorhari, on 7 Feb, 2008 - 03:26 AM, said:

What is the out put of the following program ?

main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("%d", ;
a = 5 ;
b = a++ + a++ +a++ ;
printf ( "%d",b ) ;
}

First one has pre-increments, so it's equivalent to 6+7+8 = 21
And the second will be 5+6+7 = 18 since it has post-increments

But i got 24 as the first O/P
and 15 as the second OP
How is it?

That must mean that your compiler is first incrementing all the values and then displaying in the pre-increment one (8+8+8=24)
And in the post-increment one, it's first displaying and then incrementing all the values (5+5+5=15, final value of a is 8)

This must differ from compiler to compiler. Turbo-C++ shows 21 and 18, and Bloodshed Dev-C++ shows 22 and 15 (22 because of some complications arising due to multiple '+' signs being taken as multiple increment operators; fixed that using a loop to increment).
Just out of curiosity, what IDE were you using?

There may change with compiler , But i am using TC only.