Line 38: Line 38:
  
 
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).
 
+
==Still undefined==
 
+
<syntaxhighlight lang="c" name="undefined_value" >
 +
#include<stdio.h>
 +
int main()
 +
{
 +
    int x=10;
 +
    printf("%d %d %d\n", x++, x, ++x);
 +
}
 +
</syntaxhighlight>
 +
asuresh@parambara:~$ gcc undefined.c
 +
asuresh@parambara:~$ ./a.out
 +
11 12 12
 +
asuresh@parambara:~$ icc undefined.c
 +
asuresh@parambara:~$ ./a.out
 +
10 12 12
 +
asuresh@parambara:~$ clang undefined.c
 +
asuresh@parambara:~$ ./a.out
 +
10 11 12
 
{{Template:FBD}}
 
{{Template:FBD}}
  
  
 
[[Category:Coding Questions]]
 
[[Category:Coding Questions]]

Revision as of 08:32, 26 July 2014

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

  1. include<stdio.h>

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

Solution by Arjun Suresh

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 (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 a read and a write from a memory location between two consecutive sequence points 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 between two consecutive sequence points. 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 order in which the argument expressions are processed is not defined. The only thing defined is that all arguments must be processed before the next sequence point- which here is the start of execution of the called function.

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

Still undefined

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

#include<stdio.h>

int main() {

   int x=10;
   printf("%d %d %d\n", x++, x, ++x);

} </syntaxhighlight>

asuresh@parambara:~$ gcc undefined.c 
asuresh@parambara:~$ ./a.out
11 12 12
asuresh@parambara:~$ icc undefined.c 
asuresh@parambara:~$ ./a.out
10 12 12
asuresh@parambara:~$ clang undefined.c 
asuresh@parambara:~$ ./a.out
10 11 12



blog comments powered by Disqus

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

  1. include<stdio.h>

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

Solution by Arjun Suresh[edit]

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 (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 a read and a write from a memory location between two consecutive sequence points 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 between two consecutive sequence points. 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 order in which the argument expressions are processed is not defined. The only thing defined is that all arguments must be processed before the next sequence point- which here is the start of execution of the called function.

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