4 Replies - 3578 Views - Last Post: 07 February 2008 - 07:08 AM Rate Topic: -----

#1 selloorhari  Icon User is offline

  • D.I.C Head

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

preincrement& post increment

Post icon  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",B) ;
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  Icon User is offline

  • New D.I.C Head

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

Re: preincrement& post increment

Posted 07 February 2008 - 03:35 AM

View Postselloorhari, 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",B) ;
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
Was This Post Helpful? 0
  • +
  • -

#3 selloorhari  Icon User is offline

  • D.I.C Head

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

Re: preincrement& post increment

Posted 07 February 2008 - 04:22 AM

View Postshreshth91, on 7 Feb, 2008 - 03:35 AM, said:

View Postselloorhari, 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",B) ;
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?
Was This Post Helpful? 0
  • +
  • -

#4 shreshth91  Icon User is offline

  • New D.I.C Head

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

Re: preincrement& post increment

Posted 07 February 2008 - 06:11 AM

View Postselloorhari, on 7 Feb, 2008 - 04:22 AM, said:

View Postshreshth91, on 7 Feb, 2008 - 03:35 AM, said:

View Postselloorhari, 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",B) ;
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

Was This Post Helpful? 0
  • +
  • -

#5 selloorhari  Icon User is offline

  • D.I.C Head

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

Re: preincrement& post increment

Posted 07 February 2008 - 07:08 AM

View Postshreshth91, on 7 Feb, 2008 - 06:11 AM, said:

View Postselloorhari, on 7 Feb, 2008 - 04:22 AM, said:

View Postshreshth91, on 7 Feb, 2008 - 03:35 AM, said:

View Postselloorhari, 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",B) ;
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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1