Thread: I got stuck in some coding.

  1. #1
    Registered User Rakesh o_O's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    9

    Question I got stuck in some coding.

    Code:
    #include<stdio.h>
    void main()
    {
    int x, y, z;
    x=y=z=1;
    z = (++x || ++y && ++z);  //statement 1
    printf("x = %d\ty = %d\tz = %d",x,y,z);
    }
    need help in understanding why y comes 1 instead of 2 according to me from statement 1.

  2. #2
    Registered User Rakesh o_O's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    9
    My teacher told me that it's answer is hidding in OR truth table, but i can't find any source to find its answer.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might have misheard your teacher: rather, the answer lies in the fact that && has a higher precedence than || and that for || and &&, once the left operand's value is found to be sufficient to determine the value of the expression, the right operand is not evaluated (i.e., a "short circuit" behaviour).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck with coding while Senior Project Due!!!!
    By kuro_ng in forum C Programming
    Replies: 24
    Last Post: 05-25-2011, 03:33 PM
  2. stuck on my first day of C coding (arrays)
    By viper2k in forum C Programming
    Replies: 7
    Last Post: 03-27-2011, 06:09 AM
  3. HELP - Stuck on some coding
    By trueman1991 in forum C Programming
    Replies: 6
    Last Post: 10-29-2009, 12:54 PM
  4. I'm stuck..please help me out..
    By kim_cam in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2007, 04:57 PM
  5. Stuck with '\b'
    By pratip in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2003, 07:49 AM