main()
{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n",x,y);
}
Answer Posted / 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 |
Post New Answer View All Answers
What is data structure in c language?
How can you increase the allowable number of simultaneously open files?
Explain how to reverse singly link list.
When c language was developed?
how can i access hard disk address(physical address)? are we access hard disk by using far,near or huge pointer? if yes then please explain.....
how to solve "unable to open stdio.h and conio.h header files in windows 7 by using Dos-box software
Explain what are the __date__ and __time__ preprocessor commands?
Write a program of prime number using recursion.
What is the difference between c &c++?
What is the full form of getch?
How do shell structures work?
Find duplicates in a file containing 6 digit number (like uid) in O (n) time.
How do you use a 'Local Block'?
If the size of int data type is two bytes, what is the range of signed int data type?
What is a pointer in c?