Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,422 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,250 people online right now. Registration is fast and FREE... Join Now!




preincrement& post increment

 
Reply to this topicStart new topic

preincrement& post increment

selloorhari
7 Feb, 2008 - 02:26 AM
Post #1

D.I.C Head
**

Joined: 7 Feb, 2008
Posts: 66


My Contributions
What is the out put of the following program ?



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


This post has been edited by selloorhari: 7 Feb, 2008 - 03:21 AM
User is offlineProfile CardPM
+Quote Post

shreshth91
RE: Preincrement& Post Increment
7 Feb, 2008 - 02:35 AM
Post #2

New D.I.C Head
*

Joined: 7 Feb, 2008
Posts: 4


My Contributions
QUOTE(selloorhari @ 7 Feb, 2008 - 03:26 AM) *

What is the out put of the following program ?



main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("%d",cool.gif ;
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


User is offlineProfile CardPM
+Quote Post

selloorhari
RE: Preincrement& Post Increment
7 Feb, 2008 - 03:22 AM
Post #3

D.I.C Head
**

Joined: 7 Feb, 2008
Posts: 66


My Contributions
QUOTE(shreshth91 @ 7 Feb, 2008 - 03:35 AM) *

QUOTE(selloorhari @ 7 Feb, 2008 - 03:26 AM) *

What is the out put of the following program ?



main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("%d",cool.gif ;
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?

User is offlineProfile CardPM
+Quote Post

shreshth91
RE: Preincrement& Post Increment
7 Feb, 2008 - 05:11 AM
Post #4

New D.I.C Head
*

Joined: 7 Feb, 2008
Posts: 4


My Contributions
QUOTE(selloorhari @ 7 Feb, 2008 - 04:22 AM) *

QUOTE(shreshth91 @ 7 Feb, 2008 - 03:35 AM) *

QUOTE(selloorhari @ 7 Feb, 2008 - 03:26 AM) *

What is the out put of the following program ?



main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("%d",cool.gif ;
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: 7 Feb, 2008 - 05:34 AM
User is offlineProfile CardPM
+Quote Post

selloorhari
RE: Preincrement& Post Increment
7 Feb, 2008 - 06:08 AM
Post #5

D.I.C Head
**

Joined: 7 Feb, 2008
Posts: 66


My Contributions
QUOTE(shreshth91 @ 7 Feb, 2008 - 06:11 AM) *

QUOTE(selloorhari @ 7 Feb, 2008 - 04:22 AM) *

QUOTE(shreshth91 @ 7 Feb, 2008 - 03:35 AM) *

QUOTE(selloorhari @ 7 Feb, 2008 - 03:26 AM) *

What is the out put of the following program ?



main ()
{
int a , b ;
a= 5 ;
b = ++a + ++ a + ++a ;
printf("%d",cool.gif ;
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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 11:08AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month