main()
{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n",x,y);
}

Answers were Sorted based on User's Feedback



main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / chalimar

Many of these answers depend on the results from Microsoft compiler, but I do not believe Microsoft complies with the C Standard. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

For example, in the expression x = y++ + x++;
here is Microsoft's assembler output:

mov eax, DWORD PTR _y$[ebp]
add eax, DWORD PTR _x$[ebp]
mov DWORD PTR _x$[ebp], eax
mov ecx, DWORD PTR _x$[ebp]
add ecx, 1 <<----------
mov DWORD PTR _x$[ebp], ecx <<----------
mov edx, DWORD PTR _y$[ebp]
add edx, 1
mov DWORD PTR _y$[ebp], edx

As some here have noted, Microsoft computes the expression, assigns to the lvalue, then does the post increment, X++. This does not seem to comply with the standard, which requires:

6.5.2.4 Postfix increment and decrement operators
... 2 The result of the postfix ++ operator is the value of the operand. After the result is obtained, the value of the operand is incremented. (That is, the value 1 of the appropriate type is added to it.) ... The side effect of updating the stored value of the operand shall occur between
the previous and the next sequence point.

Sequence points are an important aspect of a C program. In the Question, Microsoft does not resolve the postfix statement until after the "=" assignment, a situation that I view as a violation of the SEQUENCE POINT rule:

5.1.2.3 Program execution
...
2 ... Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place. (A summary of the sequence points is given in annex C.)

Is This Answer Correct ?    0 Yes 0 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / mahi

x=55
y=57

Is This Answer Correct ?    0 Yes 0 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / sanjev kumar sasode

56 93 (ANSI)
57 94 (Turbo C)

Is This Answer Correct ?    0 Yes 0 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / vijay

56
93

Is This Answer Correct ?    0 Yes 0 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / muhammad abdullah

#include<stdio.h>
#include<conio.h>
void main()
{
int x=20,y=35; (comment)//We delcare two variables and initialize them.
clrscr();
x=y++ + x++; (comment) /*As increment operator increases one in the value of variable.The value
of y is 35 and after increment of 1, the value of y will be 36.The value of
x=20 and after increment of 1, the value of x will be 21.Arithmetic operator
(+) adds both the values(36+21=57).So, the value of x=57.*/


y=++y + ++x; (comment) /*The value of y is 35.Due to increment operator the value of
y becomes 36.And we have ever got the value of x as it is x=57. Now,increment
operator add 1 in the value of x and the value of x will be 58.By adding
(36+58=94) we got the final value of y=94.*/
printf("The value of x=%d
The value of y=%d
",x,y);
getch();
}

Is This Answer Correct ?    0 Yes 0 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / ruchi

Answer is 57 and 94
bcoz precedence of the ++ operator is more than + operator
so y++ and x++ will be evaluated first before addition so
y++ will be 36 and x++ will be 21 after that 36 + 21 = 57
similar reasoning for y = ++y + ++x

Is This Answer Correct ?    8 Yes 9 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / deepak

57 94

Is This Answer Correct ?    3 Yes 4 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / arif shaik

main()
{
int x=20,y=35;
x=y++ + x++;//x=(y+1) + (x+1) here y=35,x=21
//x=36 + 21 = 57
//x=57 ;here previous x=21 is overwritten by 57
y=++y + ++x;//y=(1+y) + (1+x) here y=35, x=58 bcoz y
value refers y's initial address which contain 35 then
//y=(1+35) + (1+57)=36+58= 94
printf("%d %d\n",x,y);
}

Output:

57 94

Is This Answer Correct ?    2 Yes 3 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / s mahesh babu

56 and 93....

why bcz..... x=y++ + x++; in this expression xy values are post increment mean .. value not change so x=20+35-->55;

in next expression y=++y + ++x;...... so y & x pre increment ... here y=37+56.... 93

Is This Answer Correct ?    0 Yes 1 No

main() { int x=20,y=35; x = y++ + x++; y = ++y + ++x; printf(&q..

Answer / aru

x=55
y=92
ie; x= 35 + 20
ie; x=55;
then y= 56 +36
ie; y=99

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More C Interview Questions

char p="data"; printf(p);

2 Answers  


How do you print an address?

0 Answers   TCS,


What is the purpose of clrscr () printf () and getch ()?

0 Answers  


What is the difference between printf and scanf )?

0 Answers  


please help me.. how to write a code of this output?? "Enter range number:"10 1 is an odd number 2 is an even numbers 3 in an odd numbers 4 " to 10" "printing all odd numbers:" 1,3,5,7,9 "printing all even numbers:" 2,4,6,8,10 "sum of all odd numbers:25 "sum of all even numbers:30 using a C Programming ARRAY pleas pleas help.. its my project ..please :(

1 Answers  






Write a program to compute the following 1!+2!+...n!

4 Answers  


SIR PLS TELL ME THE CODE IN C LANGUAGE TO PRINT THE FOLLOWING SERIES 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1 1

4 Answers  


Explain what are header files and explain what are its uses in c programming?

0 Answers  


Write a C program where input is: "My name is xyz". output is: "xyz is name My".

1 Answers   TCS,


What is c preprocessor mean?

0 Answers  


A global variable when referred to in another file is declared as this a) local variable b) external variable c) constant d) pointers

0 Answers  


What is the difference function call by value & function call by reference?

6 Answers  


Categories