#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Unexpected Output!


    Hello.
    This is a simple piece of code i ran, and i dint get the result expected. Different compilers yield varied result.
    The snippet is:-
    Code:
    #include <stdio.h>
    
    int main() {
    	int i =1,a;
    
    	do {
    		printf("%d %d\n",i++, ++i);
    	}
    	while(i <= 10);
    
    	a = i++;
    
            printf("\n %d %d \n", a,i);
            return 0;
    }
    Apparently, the output is compiler dependent.

    Expected Output:
    1 3
    3 5
    5 7
    7 9
    9 11

    11 12


    Result, Dev C++ :
    2 2
    4 4
    6 6
    8 8
    10 10

    11 12


    Result, Code Blocks:
    2 3
    4 5
    6 7
    8 9
    10 11

    11 12


    What I wish to know is, how different compilers actually handle this code and what is (if any) the correct implementation for it?
    How could i be careful about these types of situations?

    Any help would be appreciated.
    Regards.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Spend a few minutes reading posts on undefined behavior, then stop doing stupid things.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Is it Salem who's sig contains the phrase "If you dance on the broken glass of undefined behavior, expect to get cut"?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Subhasgram,Kolkata,WB,IND
    Posts
    13
    Rep Power
    0

    Thumbs up


    Originally Posted by iammrigank
    Hello.
    This is a simple piece of code i ran, and i dint get the result expected. Different compilers yield varied result.
    The snippet is:-
    Code:
    #include <stdio.h>
    
    int main() {
    	int i =1,a;
    
    	do {
    		printf("%d %d\n",i++, ++i);
    	}
    	while(i <= 10);
    
    	a = i++;
    
            printf("\n %d %d \n", a,i);
            return 0;
    }
    Apparently, the output is compiler dependent.

    Expected Output:
    1 3
    3 5
    5 7
    7 9
    9 11

    11 12
    you got mistakes in logic not in compiler! Now run this....

    Code:
    #include<stdio.h>
    main()
    {
          int i=1,a;
          do
          {
              printf("%d_",i);
              printf("%d\n",i=i+2);   
          }
          while(i<=10);
          
          a=i++;
          printf("\n%d %d\n",a,i);
          return 0;
    }
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I very strongly recommend that you follow the links that b49P23TIvg provides you in Message #3.

    The keyword is Sequence Point.

    My analysis of the same kind of problem is at http://pgm.dwise1.net/misc/test.html.

    Now this has been given far more attention than it deserves.

IMN logo majestic logo threadwatch logo seochat tools logo