5 Replies - 169 Views - Last Post: 07 February 2012 - 10:35 AM Rate Topic: -----

Topic Sponsor:

#1 Ana24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 12

What is the output of the following program ?

Posted 07 February 2012 - 08:28 AM

int  a = 5;
int b = a++;
int c = ++a;
printf ("%d %d %d\n", a, b, c);
return 0



I don't understand how the out put is supposed to be 7 5 7 when the first is a and a=5
Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: What is the output of the following program ?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,892
  • Joined: 27-December 08

Re: What is the output of the following program ?

Posted 07 February 2012 - 08:30 AM

The a++ and ++a operations increment the value stored in the variable. The a++ operation evaluates after the value of a (5) is assigned to b. The ++a operation is executed before the value of a (now 7) is assigned to c.

Also, remember to end your return statement with a semi-colon.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: What is the output of the following program ?

Posted 07 February 2012 - 08:31 AM

The variable 'a' starts at five then you increment 'a' twice. After the increments 'a' will be seven.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 Ana24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 12

Re: What is the output of the following program ?

Posted 07 February 2012 - 08:49 AM

View Postmacosxnerd101, on 07 February 2012 - 08:30 AM, said:

The a++ and ++a operations increment the value stored in the variable. The a++ operation evaluates after the value of a (5) is assigned to b. The ++a operation is executed before the value of a (now 7) is assigned to c.

Also, remember to end your return statement with a semi-colon.


Thanks, but shouldn't it be 5 5 7 ? I don't understand how the first changes...
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: What is the output of the following program ?

Posted 07 February 2012 - 09:02 AM

Here is the same program without the increment operators:
/*
int  a = 5;
int b = a++;
int c = ++a;
printf ("%d %d %d\n", a, b, c);
return 0;
*/
int a = 5;
int b = a;
a = a + 1; // to replace a++;
a = a + 1; // This will replace the ++a
c = a;
printf ("%d %d %d\n", a, b, c);




Jim
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,892
  • Joined: 27-December 08

Re: What is the output of the following program ?

Posted 07 February 2012 - 10:35 AM

The variable a is 7 at the point of the printf() statement, the variable b takes on 5 before a becomes 6, then a becomes 7 and that value is assigned to c.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1