Thread: help me

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    help me

    How to find the value of z here

    Int x = 5, y = 6, Z;
    Z = x + + > (Z = - - Y)? Y + 2: ((x>z) ? x:y);

    and why it prints 18 in the following programme-
    Code:
    int main()
    {
    int x,y=10;
    x=y++ +--y;
    printf("%d",x);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Any single line of code with any single variable being incremented or decremented twice, is at best, unclear.

    At worst, it's non-standard, compiler and/or machine dependent, and something best left for after hours fun, when the beer is flowing.

    Don't waste your time messing with it, is my strong suggestion.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    Int x = 5, y = 6, Z;
    Z = x + + > (Z = - - Y)? Y + 2: ((x>z) ? x:y);
    Well, first, you need a value for z (not Z, z) and then you can use the table here to guide you through the operation order.

    Edit, for that matter, you need a value for Y too. Variable names are case-sensitive, so if you're wanting to use just x, y and z, keep it all lower-case or upper-case.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adak View Post
    Any single line of code with any single variable being incremented or decremented twice, is at best, unclear.
    In fact it is undefined, as someone pointed out last week. So this line:
    Code:
    x=y++ +--y;
    is simply no good -- altho y-1=9 and 9+9 does equal 18, you should NOT use "--" or "++" more than once on the same variable in the same expression.

    That other line is okay I think if you set the variable names correctly:
    Code:
    z = x++ > (z = --y)? y+2 : (x>z)?x:y;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by MK27 View Post
    In fact it is undefined, as someone pointed out last week. So this line:
    Code:
    x=y++ +--y;
    is simply no good -- altho y-1=9 and 9+9 does equal 18, you should NOT use "--" or "++" more than once on the same variable in the same expression.
    But yet its giving the value as 18 rite?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    and why it prints 18 in the following programme-
    Code:
    int main()
    {
    int x,y=10;
    x=y++ +--y;
    printf("%d",x);
    }
    chk in the code

    Code:
    x = y++ + --y
    here y is incremented after everything is done...while its decremented before anything is done

    so the initial value of it is 10....then it becomes 9...(still its not incremented)..then it becomes 9+9 =x..after equating ..then the y value is incremented...so the answer is 18

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Obelisk View Post
    here y is incremented after everything is done...while its decremented before anything is done

    so the initial value of it is 10....then it becomes 9...(still its not incremented)..then it becomes 9+9 =x..after equating ..then the y value is incremented...so the answer is 18
    No. Try reading what wiser people have already posted. You cannot use multiple ++ or -- operators (of combination thereof) on the same variable in the same statement and get defined results.

    Also, if you want to be taken seriously, stop typing "y" and "chk" instead of "why" and "check". I'm not going to go through the whole dictionary for you. If you can't be bothered to type the whole word, don't bother typing at all! Some things are meant to be abbreviated. Those are not. If you're posting on a friggin' phone, go away until you can get to a real keyboard.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by quzah View Post
    No. Try reading what wiser people have already posted. You cannot use multiple ++ or -- operators (of combination thereof) on the same variable in the same statement and get defined results.

    Also, if you want to be taken seriously, stop typing "y" and "chk" instead of "why" and "check". I'm not going to go through the whole dictionary for you. If you can't be bothered to type the whole word, don't bother typing at all! Some things are meant to be abbreviated. Those are not. If you're posting on a friggin' phone, go away until you can get to a real keyboard.


    Quzah.
    and btw the only abbrevation i used in my previous post was "chk" ...y is the variable in the code....Though my piece of code is faulty .
    my apologies.....i'll be more careful in posting next time....thanks for the piece of advice
    Last edited by Obelisk; 10-19-2009 at 01:27 PM.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Obelisk View Post
    and btw the only abbrevation i used in my previous post was "chk" ...y is the variable in the code....Though my piece of code is faulty .
    my apologies.....i'll be more careful in posting next time....thanks for the piece of advice
    Not to mention the use of 'rite' instead of 'right', lack of punctuation (eg: missing periods and whatnot), not capitalizing sentences, and inconsistent use of spacing and certain punctuation (eg: '..', '...', and '....'). Not that it matters all that much, but it just looks sloppy and leaves a bad impression of your posts.

Popular pages Recent additions subscribe to a feed