Line 1: | Line 1: | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
− | int x=10, y; | + | #include<stdio.h> |
− | y=x++ + ++x; | + | int main() |
− | printf("%d %d %d %d " y, x++, x, ++x); | + | { |
+ | int x=10, y; | ||
+ | y = x++ + ++x; | ||
+ | printf("%d %d %d %d ", y, x++, x, ++x); | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
a)22,10,11,13 | a)22,10,11,13 | ||
+ | |||
b)22,11,11,11 | b)22,11,11,11 | ||
+ | |||
c)12,10,11,13 | c)12,10,11,13 | ||
+ | |||
d)22 13 13 13 | d)22 13 13 13 | ||
+ | None of the choices are correct as per [http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf C standard]. This is because the statement | ||
+ | y=x++ + ++x; | ||
+ | |||
+ | can causes undefined behavior, so does the statement | ||
+ | printf("%d %d %d %d ", y, x++, x, ++x); | ||
+ | |||
+ | C standard says that the side effects of an operation should be complete 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 (running faster). But as a consequence, programmer shouldn't do a read + write from a memory location within a sequence point or otherwise the result would be undefined. | ||
+ | |||
+ | In the statement, | ||
+ | y = x++ + ++x; | ||
+ | |||
+ | There are 2 reads to the memory location of x and 2 writes to the same within a sequence point. So, the result of this statement has no guarantee as per C standard. | ||
+ | |||
+ | Similarly, the statement | ||
+ | printf("%d %d %d %d ", y, x++, x, ++x); | ||
+ | |||
+ | is undefined. In a function call in C, the arguments are pushed onto the stack from right to left (the left most argument would be on top of the stack, so that variable no. of arguments can work in C). But the order in which the argument expressions are processed is not defined. The only thing defined is that all arguments must be processes before the next sequence point- which here is the execution of the called function. | ||
+ | |||
+ | |||
+ | |||
+ | |||
− | |||
[https://en.wikipedia.org/wiki/Sequence_point Sequence Point] | [https://en.wikipedia.org/wiki/Sequence_point Sequence Point] | ||
− | + | <disqus/> | |
[[Category:Code]] | [[Category:Code]] |
<syntaxhighlight lang="c">
int main() {
int x=10, y; y = x++ + ++x; printf("%d %d %d %d ", y, x++, x, ++x);
} </syntaxhighlight>
a)22,10,11,13
b)22,11,11,11
c)12,10,11,13
d)22 13 13 13
None of the choices are correct as per C standard. This is because the statement
y=x++ + ++x;
can causes undefined behavior, so does the statement
printf("%d %d %d %d ", y, x++, x, ++x);
C standard says that the side effects of an operation should be complete only before the next sequence point. This relaxation is given so that the compiler would be able to generate the most optimal code (running faster). But as a consequence, programmer shouldn't do a read + write from a memory location within a sequence point or otherwise the result would be undefined.
In the statement,
y = x++ + ++x;
There are 2 reads to the memory location of x and 2 writes to the same within a sequence point. So, the result of this statement has no guarantee as per C standard.
Similarly, the statement
printf("%d %d %d %d ", y, x++, x, ++x);
is undefined. In a function call in C, the arguments are pushed onto the stack from right to left (the left most argument would be on top of the stack, so that variable no. of arguments can work in C). But the order in which the argument expressions are processed is not defined. The only thing defined is that all arguments must be processes before the next sequence point- which here is the execution of the called function.
<syntaxhighlight lang="c">
int main() {
int x=10, y; y = x++ + ++x; printf("%d %d %d %d ", y, x++, x, ++x);
} </syntaxhighlight>
a)22,10,11,13
b)22,11,11,11
c)12,10,11,13
d)22 13 13 13
None of the choices are correct as per C standard. This is because the statement
y=x++ + ++x;
can causes undefined behavior, so does the statement
printf("%d %d %d %d ", y, x++, x, ++x);
C standard says that the side effects of an operation should be complete only before the next sequence point. This relaxation is given so that the compiler would be able to generate the most optimal code (running faster). But as a consequence, programmer shouldn't do a read + write from a memory location within a sequence point or otherwise the result would be undefined.
In the statement,
y = x++ + ++x;
There are 2 reads to the memory location of x and 2 writes to the same within a sequence point. So, the result of this statement has no guarantee as per C standard.
Similarly, the statement
printf("%d %d %d %d ", y, x++, x, ++x);
is undefined. In a function call in C, the arguments are pushed onto the stack from right to left (the left most argument would be on top of the stack, so that variable no. of arguments can work in C). But the order in which the argument expressions are processed is not defined. The only thing defined is that all arguments must be processes before the next sequence point- which here is the execution of the called function.