Thread: freaky output!

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    30

    freaky output!

    Can any one tell me why the out put is "1 40 0" and not "1 40 1"
    Code:
    main()
    {
         int x;
         printf("%d%d%d",x<=50,x=40,x>=10);
         getch();
    }
    cheers!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's undefined.

    The standard emposes no order of which arguments are evaulated, that's up to the compiler (and optimization it might do*).

    * Which may change the result when optimizations are on or off for example.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    You have not initialized x so it's value is undefined. Also it is undefined what the order of argument evaluation is, so you do not know whether or not x=40 happens before x>=10, and obviously in this case it doesn't.

    Why would you be doing something like this?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The obvious "fix" is;

    Code:
    int main(void)
    {
       int x = 2;
       int a, b;
    
       a = (x <= 50);
       x = 40;
       b = (x >= 10);
    
       printf("&#37;d %d %d\n", a, x, b);
       return 0;
    }

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Can any one tell me why the out put is "1 40 0" and not "1 40 1"
    it completely depends on the compiler used.also as x is undefined , any garbage value is taken up and according to this value & the order of argument evaluation the compiler produces the output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM