1

Following is the code that I run in ubuntu 13.10. Code:-

    `#include<stdio.h>
     main()
     {
         int i=10,j=10;
         i=i++ + ++j;
         printf("i=%d j=%d\n",i,j);
         j=++i + j++;
         printf("i=%d j=%d\n",i,j);
     }

Output:-

    i=21 j=11 
    i=22 j=33

Logically,as per rules ans should be:-

    i=22 j=11
    i=23 j=35

And when I run this code in ubuntu 12.10 then i get correct ans i.e. above ans. Please explain what is happening??

Jinal
  • 81
  • 1
  • 3
  • 8
  • 1
    I'm pretty sure this code is invoking undefined behavior. Look into sequence points. – cf- Mar 22 '14 at 04:55
  • @computerfreaker Please explain me why is it invoking undefined behaviour ? and how to look into sequence points? – Jinal Mar 22 '14 at 05:03
  • 1
    It's invoking undefined behavior because the language standard says you can change a variable's value at most one time between sequence points, which are sort of steps in the program. You would look into sequence points the same way you look into anything else: Google it. @antlersoft provided you a nice explanation, too. – cf- Mar 22 '14 at 05:08
  • Ok thanks @computerfreaker.But can you explain me what should be the correct ans of given code with explanation.Please. – Jinal Mar 22 '14 at 05:20

0 Answers0