Like Tree1Likes
  • 1 Post By laserlight

I got stuck in some coding.

This is a discussion on I got stuck in some coding. within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> void main() { int x, y, z; x=y=z=1; z = (++x || ++y && ++z); //statement 1 printf("x ...

  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
    21,436
    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).
    Rakesh o_O likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06:49 AM

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