What will be the output of the following code?

This is a discussion on What will be the output of the following code? within the C++ Programming forums, part of the General Programming Boards category; 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; ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Have you compiled and run it?

    int main()
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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 Floating Values acting weird.
    for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    ZuK
    ZuK is offline
    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,295
    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; 12-31-2006 at 11:05 PM.

  12. #12
    Registered User developersubham's Avatar
    Join Date
    Dec 2006
    Location
    Kolkata (India)
    Posts
    8
    Quote Originally Posted by vart
    Read this Floating Values acting weird.
    for example
    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-15-2002, 11:04 PM
  4. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21