Thread: What will be the output of the following code?

  1. #1
    Registered User developersubham's Avatar
    Join Date
    Dec 2006
    Location
    Kolkata (India)
    Posts
    8

    What will be the output of the following code?

    What will be the output of the following code and WHY?

    Code:
    #include <stdio.h>
    
    void main()
    {
    int x,y,z;
    x=y=z=1;
    z=++x||++y&&++z;
    printf("x=%d y=%d z=%d\n",x,y,z);
    }
    Code:
    #include <stdio.h>
    
    void main()
    {
    float a=0.7
    if (0.7>a)
    printf("Hi");
    else
    printf("Hello");
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Have you compiled and run it?

    int main()
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    yes i have executed it.
    So? Is the output as you expect? Or not? And you don't understand why not?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User developersubham's Avatar
    Join Date
    Dec 2006
    Location
    Kolkata (India)
    Posts
    8
    Quote Originally Posted by vart
    Have you compiled and run it?

    int main()
    int main() is always not needed. void main() is also correct. The program has output.
    yes i have executed it.

  5. #5
    Registered User developersubham's Avatar
    Join Date
    Dec 2006
    Location
    Kolkata (India)
    Posts
    8
    Quote Originally Posted by vart
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376


    So? Is the output as you expect? Or not? And you don't understand why not?
    No, the output is not as expected. I can't understand it.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    I see no apparent mistake.

    Perhaps the compiler doesn't allow the increment operator to be applied more than once on the same sentence that uses && and ||. Switching x and y results in y having a value of 2 and x of 1 (as opposed to x = 2 and y = 1) so only the first variable is actually incremented. if we use a fourth variable 'w' to receive the assignment of the operation, 'w' receives the value of 1 and z is not incremented (when we print all four variables). Apparently incrementing more than one variable is not even recognized by the compiler. Then again, that's just my educated guess.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by developersubham
    No, the output is not as expected. I can't understand it.
    Look at the following code
    Code:
    if(pThing && pThing->bUsed)
    What do you think will happen if pThing is NULL?
    Try to figure out why...
    Try to figure out what is happening in your case
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    as for the other part...

    The other part is a good puzzle. I think only an expert could answer for sure, why the computer says 0.7 is greater than the value of a variable initialized by

    Code:
      float a = 0.7;
    Could it be the internal representation of both numbers (a constant and a variable)? or possibly how the > operator internally in the microarquitecture. I have no clue, but it does lead to think about what lies below the compiler and the executed code.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by jalnewbie
    The other part is a good puzzle. I think only an expert could answer for sure, why the computer says 0.7 is greater than the value of a variable initialized by

    Code:
      float a = 0.7;
    Could it be the internal representation of both numbers (a constant and a variable)? or possibly how the > operator internally in the microarquitecture. I have no clue, but it does lead to think about what lies below the compiler and the executed code.
    Read this http://cboard.cprogramming.com/showt...ight=scientist
    for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by jalnewbie
    The other part is a good puzzle. I think only an expert could answer for sure, why the computer says 0.7 is greater than the value of a variable initialized by

    Code:
      float a = 0.7;
    Could it be the internal representation of both numbers (a constant and a variable)? or possibly how the > operator internally in the microarquitecture. I have no clue, but it does lead to think about what lies below the compiler and the executed code.
    Try this and try to figure out the difference
    Code:
    #include <stdio.h>
    
    int main()
    {
        float a=0.7;
        if (0.7f>a)
            printf("Hi");
        else
            printf("Hello");
    }
    Kurt

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by developersubham
    int main() is always not needed. void main() is also correct. The program has output.
    yes i have executed it.
    Don't talk such nonsense!
    Read this: http://www.research.att.com/~bs/bs_faq2.html#void-main

    Also, you're lying! the second program is missing a semi-colon and CANNOT compile. If you want to know why it does what it does when you add the missing semicolon, you'll need to read this:
    http://docs.sun.com/source/806-3568/ncg_goldberg.html

    This line has what is called "undefined behaviour":
    z=++x||++y&&++z;
    You may not modify a variable more than once without intervening sequence points.
    See http://msdn.microsoft.com/library/de...m/expre_11.asp.
    Last edited by iMalc; 01-01-2007 at 12:05 AM.

  12. #12
    Registered User developersubham's Avatar
    Join Date
    Dec 2006
    Location
    Kolkata (India)
    Posts
    8
    Quote Originally Posted by vart
    thanks for the help.

  13. #13
    Registered User developersubham's Avatar
    Join Date
    Dec 2006
    Location
    Kolkata (India)
    Posts
    8
    Quote Originally Posted by jalnewbie
    Perhaps the compiler doesn't allow the increment operator to be applied more than once on the same sentence that uses && and ||. Switching x and y results in y having a value of 2 and x of 1 (as opposed to x = 2 and y = 1) so only the first variable is actually incremented. if we use a fourth variable 'w' to receive the assignment of the operation, 'w' receives the value of 1 and z is not incremented (when we print all four variables). Apparently incrementing more than one variable is not even recognized by the compiler. Then again, that's just my educated guess.
    I also thought like you. But need clarifications. Thanks for giving a try.

  14. #14
    Registered User developersubham's Avatar
    Join Date
    Dec 2006
    Location
    Kolkata (India)
    Posts
    8

    Compiler doesn't check if it has got the answer.

    Quote Originally Posted by jalnewbie
    Perhaps the compiler doesn't allow the increment operator to be applied more than once on the same sentence that uses && and ||. Switching x and y results in y having a value of 2 and x of 1 (as opposed to x = 2 and y = 1) so only the first variable is actually incremented. if we use a fourth variable 'w' to receive the assignment of the operation, 'w' receives the value of 1 and z is not incremented (when we print all four variables). Apparently incrementing more than one variable is not even recognized by the compiler. Then again, that's just my educated guess.
    If a left part of the || is +ve, it is obvious that the output will be 1 therfore compiler doesnot calculate the other side of the ||. It ignores it.
    Similarly if left side of && is 0 the result is obviously 0 therfore compiler discards the other part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  4. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM