Arjun Suresh (talk | contribs) |
Arjun Suresh (talk | contribs) |
||
Line 13: | Line 13: | ||
===Solution=== | ===Solution=== | ||
− | The answer to this is "Undefined". C standard says that the side effects of an operation (for b = ++a; modification of a is a side-effect) need to be completed only before the next [https://en.wikipedia.org/wiki/Sequence_point sequence point]. This relaxation is given so that the compiler would be able to generate the most optimal code (which run faster). But as a consequence, programmer shouldn't do | + | The answer to this is "Undefined". C standard says that the side effects of an operation (for b = ++a; modification of a is a side-effect) need to be completed only before the next [https://en.wikipedia.org/wiki/Sequence_point sequence point]. This relaxation is given so that the compiler would be able to generate the most optimal code (which run faster). But as a consequence, programmer shouldn't do multiple writes to a memory location within a sequence point or otherwise the result would be undefined. |
+ | Between two sequence points, an object is modified more than once, or is modified and the prior value is read other than to determine the value to be stored | ||
So, in the statement a = ++a / a++; | So, in the statement a = ++a / a++; |
<syntaxhighlight lang="c">
int main() {
int a = 2; a = ++a / a++; printf("%d", a); return 0;
} </syntaxhighlight>
The answer to this is "Undefined". C standard says that the side effects of an operation (for b = ++a; modification of a is a side-effect) need to be completed only before the next sequence point. This relaxation is given so that the compiler would be able to generate the most optimal code (which run faster). But as a consequence, programmer shouldn't do multiple writes to a memory location within a sequence point or otherwise the result would be undefined. Between two sequence points, an object is modified more than once, or is modified and the prior value is read other than to determine the value to be stored
So, in the statement a = ++a / a++;
There are two side effect on a, which can happen anytime before the next sequence point. So, as per C standard, the output is simply UNDEFINED.
<syntaxhighlight lang="c">
int main() {
int a = 2; a = ++a / a++; printf("%d", a); return 0;
} </syntaxhighlight>
The answer to this is "Undefined". C standard says that the side effects of an operation (for b = ++a; modification of a is a side-effect) need to be completed only before the next sequence point. This relaxation is given so that the compiler would be able to generate the most optimal code (which run faster). But as a consequence, programmer shouldn't do multiple writes to a memory location within a sequence point or otherwise the result would be undefined. Between two sequence points, an object is modified more than once, or is modified and the prior value is read other than to determine the value to be stored
So, in the statement a = ++a / a++;
There are two side effect on a, which can happen anytime before the next sequence point. So, as per C standard, the output is simply UNDEFINED.