Thursday, July 9, 2009

What is the output of the following program in c and how? int i=3,j=2;printf("%d",i++ + ++j + --i + j--);?

what is the difference in c program





int i=3,j=2;


1)printf("%d",i++ + ++j + --i + j--);


and


2)p=i++ + ++j + --i + j--;


printf("%d",p);

What is the output of the following program in c and how? int i=3,j=2;printf("%d",i++ + ++j + --i + j--);?
well i can't remember c well but i know its similar to java





1-it is printing out the results of the variables from the formula given





2- it is assigning that formula to the variable P and then printing it out
Reply:Without actually trying it, I would think both would result in the same output. The main difference is the result in the second example is stored in p.





As for the value, since there are no parenthesis involved to modify order of processing, they are done in order left-to-right.


a) First, i is set to 3 and j to 2


b) i++ is added to the accumulator. Since it is a postfix ++, the value of 3 is added to accumulator before i is incremented to 4.


c) ++j is added to the accumulator. Since this is a prefix, 3 (the new value of j) is added in, making the accumulator 6.


d) --i makes the accumulator 8 after setting i back to 3.


e) j-- makes the accumulator 11 and then sets j back to 2.





In both cases, i and j are back at their original values and 11 is displayed (and stored, in the second example).
Reply:on is setting it into a variable before submitting it to the complier





ex p= %26lt;--- thatas a variable and he is just echoing it out... make sense?
Reply:The question is wrong. Output will be undefined. But some stubborn teachers don't agree.





There is a similar question which I asked in C forum. Read this :





http://cboard.cprogramming.com/showthrea...








http://www.research.att.com/~bs/bs_faq2....
Reply:the first printf prints 12


and the second prints 10
Reply:1) 12


2) 12
Reply:The answer for the first is it displays 12 and for the second undefined variable 'p'. because in "C" we have to declare a variable before we using it.


No comments:

Post a Comment