Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: C Question

  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    907
    Thanks
    301
    Thanked 2 Times in 2 Posts

    Question C Question

    Why is the following C code outputting 1011? Whats the logic?

    Code:
    #include<stdio.h>
    int main()
    {
        int i=4,j=-1,k=0,w,x,y,z;
        w=i||j||k;
        x=i&&j&&k;
        y=i||j&&k;
        z=i&&j||k;
        printf("%d%d%d%d",w,x,y,z);
        getch();
    }

    Thanx

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    That is just the results of logical and and logical or operations. They can either be a 1 or a 0 and then it prints them all out together with that printf.
    OracleGuy

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    907
    Thanks
    301
    Thanked 2 Times in 2 Posts
    yes but i need to know whats the logic behind it

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    See http://en.wikipedia.org/wiki/Logical...er_programming
    (4 is equivalent to 0100 in binary)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Code:
    int i=4,j=-1,k=0,w,x,y,z;
    w=i||j||k; True OR True OR False = True
    x=i&&j&&amp;k; True AND True AND False = False
    y=i||j&&amp;k; True AND True AND False = True
    z=i&&j||k; True AND True OR False = True
    See:
    http://en.wikipedia.org/wiki/Logical_conjunction
    http://en.wikipedia.org/wiki/Logical_disjunction
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •