INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
DETAILS
Question: void main() { int i=20;j=35,x,y; x= y++ + x++; y= ++y + ++x; printf("
%d %d" , x,y); } /* please tell me the value of x and y at each and every moment. i dont need the answer but the explanation of the anamolus behavior of increment expression.*/
Answer: x=1702 y=2560
|
Question:
void main() { int i=20;j=35,x,y; x= y++ + x++; y= ++y + ++x; printf("
%d %d" , x,y); } /* please tell me the value of x and y at each and every moment. i dont need the answer but the explanation of the anamolus behavior of increment expression.*/
Answer:
x=1702 y=2560 Source: CoolInterview.com
Answered by: logesh | Date: 7/12/2008
| Contact logesh
First i explain how the incremental operator works Y++ means increase the value of y by one. the value of y is incremented by one after the first iteration.
++Y means the value of y is first incremented before iteration and value of y is incremented and then the expression evaluates.
In the above expression the result is :
No value is assigned for x & y so when we declare the variables without assigning the values default values may be present so based on that default values the incremental operator works.
suppose the default values of x & y are 10 & 20 then the expression
x=y++ + x++ x=10+20; x=30
y=++y + ++X y=11 + 21; y=32 Source: CoolInterview.com
Answered by: sarathy C.R | Date: 7/13/2008
| Contact sarathy C.R
x=y++ + x++ x=10+20; this gives x=30 but x has been post incremented so calue of x becomes 31.
y=++y + ++X y=21 + 32; y=53 since x is pre-incremented so value of x becomes 32,and by the same reason y becomes 21 . Source: CoolInterview.com
Answered by: krishna | Date: 11/11/2009
| Contact krishna
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|