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

Write a c program to print the even numbers followed by odd numbers in an array without using additional array

1 Answers   Tech Mahindra,


Is null valid for pointers to functions?

0 Answers  


What is the difference between static and global variables?

1 Answers  


What is indirection in c?

0 Answers  


Why is c called a mid-level programming language?

0 Answers  






Which of the following data structures is on average the fastest for retrieving data: 1) Binary Tree 2) Hash Table 3) Stack

3 Answers  


What are the restrictions of a modulus operator?

0 Answers  


Why main is used in c?

0 Answers  


Which are low level languages?

0 Answers  


what is the meaning of java that is (J A V A) full form of JAVA

71 Answers   AKS University, Bhel, BNL, BPO, HCL, Peacecon,


What does c value mean?

0 Answers  


What are disadvantages of C language.

0 Answers   iNautix,


Categories