(Solution)
Line 21: Line 21:
 
So, in the statement a = ++a;
 
So, in the statement 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'''.
+
There is a side effect on a, which can happen anytime before the next sequence point. So, as per C standard, the output is simply '''UNDEFINED'''.
  
 
  Undefined value means compiler can give any value. i.e.; different compilers or even different versions of the same compiler can give different answers. As per C standard, no programmer should write this code. This is different from compiler dependent, in which case the output is clearly defined by the compiler and programmer can write those code as long as he is aware of the compiler. Example of compiler dependent code is sizeof(int).
 
  Undefined value means compiler can give any value. i.e.; different compilers or even different versions of the same compiler can give different answers. As per C standard, no programmer should write this code. This is different from compiler dependent, in which case the output is clearly defined by the compiler and programmer can write those code as long as he is aware of the compiler. Example of compiler dependent code is sizeof(int).

Revision as of 14:40, 9 March 2014

What's the output?

<syntaxhighlight lang="c" name="undefined_value_2">

  1. include <stdio.h>

int main() {

  int a = 2;
  a = ++a;
  printf("%d", a);
  
  return 0;

} </syntaxhighlight>

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 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.

Undefined behavior can occur if 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;

There is a side effect on a, which can happen anytime before the next sequence point. So, as per C standard, the output is simply UNDEFINED.

Undefined value means compiler can give any value. i.e.; different compilers or even different versions of the same compiler can give different answers. As per C standard, no programmer should write this code. This is different from compiler dependent, in which case the output is clearly defined by the compiler and programmer can write those code as long as he is aware of the compiler. Example of compiler dependent code is sizeof(int).




blog comments powered by Disqus

What's the output?[edit]

<syntaxhighlight lang="c" name="undefined_value_2">

  1. include <stdio.h>

int main() {

  int a = 2;
  a = ++a;
  printf("%d", a);
  
  return 0;

} </syntaxhighlight>

Solution[edit]

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.

Undefined behavior can occur if 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;

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.

Undefined value means compiler can give any value. i.e.; different compilers or even different versions of the same compiler can give different answers. As per C standard, no programmer should write this code. This is different from compiler dependent, in which case the output is clearly defined by the compiler and programmer can write those code as long as he is aware of the compiler. Example of compiler dependent code is sizeof(int).




blog comments powered by Disqus