GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 395 of 453    Print  
a=5;
c=++a + ++a + ++a;

What would be value of c after this statement and how does it come?

If we have the code
a=5;
c=a++ + a++ + a++;

What would be the value of c then?

  
Total Answers and Comments: 19 Last Update: October 24, 2008     Asked by: ashwini_nith 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: srisujan
 
The value of c will be 24

because ++a being a pre incrementing operator it will increment the value of a before the expression being evaluvated..

thereby

c = ++5 + ++5 + ++5

c = 6          7         8       //this line specifies the value of a after incrementing and the last updated value is 8.

now evaluatin the expression

c = 8 + 8 + 8

c=24

Above answer was rated as good by the following members:
rajani_vaddepalli15
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
November 25, 2007 05:47:02   #1  
srisujan        

RE: a=5;c=++a + ++a + ++a;What would...
The value of c will be 24

because ++a being a pre incrementing operator it will increment the value of a before the expression being evaluvated..

thereby

c ++5 + ++5 + ++5

c 6 7 8 //this line specifies the value of a after incrementing and the last updated value is 8.

now evaluatin the expression

c 8 + 8 + 8

c 24

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
November 25, 2007 13:42:46   #2  
Kamal        

RE: a=5;c=++a + ++a + ++a;What would...
In the above case a single variable is used multiple times here 'a'.

Thus the output is not guarantee the output. It can be anything according to different increment and decrement behavior.

Such kind of statements should be avoided.

Regards
Kamal

 
Is this answer useful? Yes | No
November 26, 2007 11:18:16   #3  
ashwini_nith Member Since: July 2007   Contribution: 6    

RE: a=5;c=++a + ++a + ++a;What would...
Well executing the line
c ++a + ++a + ++a;

Gives the output as 22.
BUt I am not sure why it does so???



 
Is this answer useful? Yes | No
November 27, 2007 01:12:00   #4  
yehsi        

RE: a=5;c=++a + ++a + ++a;What would...

if
a 5;
c ++a + ++a + ++a;

a 8;
c 6+7+8
22;

if
a 5;
c a++ + a++ + a++;

a 8;
c 5+5+5
15;


 
Is this answer useful? Yes | No
November 29, 2007 07:12:27   #5  
kamal7725 Member Since: November 2007   Contribution: 15    

RE: a=5;c=++a + ++a + ++a;What would be value of c after this statement and how does it come?If we have the codea=5;c=a++ + a++ + a++;What would be the value of c then?
value will be 24 i.e
8 + 8 +8

why?

because compiler will add them after increment a's value. In this case compiler make two stack like
++a
++a +
++a +

and arithmetic operation will take place after evaluating final value of a and that would be 8
now it time to made some mathematical compiler take the operand i.e 8 and pop an operator '+' from the stack so the expression would be
8 + 8 + 8 24;

 
Is this answer useful? Yes | No
December 04, 2007 01:35:03   #6  
busybee Member Since: August 2007   Contribution: 2    

RE: a=5;c=++a + ++a + ++a;What would be value of c after this statement and how does it come?If we have the codea=5;c=a++ + a++ + a++;What would be the value of c then?
case i : (c ++a + ++a + ++a;)
c 8+7+6
c 21

case ii : (c a++ + a++ + a++;)
c 7+6+5
c 18

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
January 22, 2008 06:33:22   #7  
shetty.gprakash Member Since: January 2008   Contribution: 1    

RE: a=5;c=++a + ++a + ++a;What would be value of c after this statement and how does it come?If we have the codea=5;c=a++ + a++ + a++;What would be the value of c then?
For the first one answer c 8 + 8 + 8 24 and a 8

for the second one c 5 + 5 + 5 15 and a 8

 
Is this answer useful? Yes | No
January 23, 2008 06:09:11   #8  
sonalind Member Since: January 2008   Contribution: 1    

RE: a=5;c=++a + ++a + ++a;What would be value of c after this statement and how does it come?If we have the codea=5;c=a++ + a++ + a++;What would be the value of c then?
For first case value of the C will be 22.For 2nd case it is 15. Consider the first case:C ++a + ++a + ++a; The above calculation will be carried out in the following sequence: 1) TempMem (++a + ++a) (pre-increment operator has a preference so before addition value of the “a” is incremented twice and it became 7) so result of the eq1 is 14. 2) Again value of the ‘a’ is incremented and now it is 8. Now the remaining part of the instruction will be carried out. 3) TempMem TempMem + 8 22 4) C TempMem Consider the second case:C a++ + a++ + a++; post-increment operator has lower preference so the value of ‘a’ will be incremented thrice after addition but for an addition the value will be unchanged i.e. 5.
 
Is this answer useful? Yes | No
January 25, 2008 00:42:19   #9  
rajeevdas2000 Member Since: January 2008   Contribution: 2    

I am sure for my answer
when a 5then c ++a + ++a + ++a 24(8+8+8)(last value of a will be 8)
 
Is this answer useful? Yes | No
January 27, 2008 07:17:44   #10  
milindd Member Since: January 2008   Contribution: 1    

RE: a=5;c=++a + ++a + ++a;What would be value of c after this statement and how does it come?If we have the codea=5;c=a++ + a++ + a++;What would be the value of c then?

Compile this code and see answer is 21 (if a 5)

Regards


 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape