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
Explain the properties of union.
Why c is called procedure oriented language?
What are the rules for identifiers in c?
what are the different storage classes in c?
Implement bit Array in C.
Explain the difference between #include "..." And #include <...> In c?
What is array within structure?
I have seen function declarations that look like this
What is omp_num_threads?
What are the advantages of union?
stripos — Find position of first occurrence of a case- insensitive string int stripos ( char* haystack, char* needle, int offset ) Returns the numeric position of the first occurrence of needle in the haystack string. Note that the needle may be a string of one or more characters. If needle is not found, stripos() will return -1. The function should not make use of any C library function calls.
Is it fine to write void main () or main () in c?
How can you access memory located at a certain address?
Explain what is wrong with this program statement?
Write a function that will take in a phone number and output all possible alphabetical combinations