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

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal. [ I gave the obvious solution of taking % 10 and / 10, which gives us the decimal value in reverse order. This requires an array since we need to print it out in the correct order. The interviewer wasn't too pleased and asked me to give a solution which didn't need the array ].

0 Answers  


how to write optimum code to divide a 50 digit number with a 25 digit number??

0 Answers   MGM,


Suppose I want to write a function that takes a generic pointer as an argument and I want to simulate passing it by reference. Can I give the formal parameter type void **, and do something like this? void f(void **); double *dp; f((void **)&dp);

1 Answers  


A stack can be implemented only using array?if not what is used?

3 Answers   InterGlobal,


write a c program to find the square of a 5 digit number and print the result.

5 Answers   Accenture, Sasken, Vimukti Technologies,






How to removing white spces in c programming only bu using loops

2 Answers  


Explain the binary height balanced tree?

0 Answers  


Do pointers take up memory?

0 Answers  


Define a structure to store roll no, name and marks of a student. Using the structure of above write a ‘C’ program to create a file “student.dat”. There must be one record for every student in the file. Accept the data from the user.

0 Answers  


What is 02d in c?

0 Answers  


how to TOGGLE Nth bit of variable in a MACRO

1 Answers   NDS,


What is difference between function overloading and operator overloading?

0 Answers  


Categories