Thread: Explain this program please

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Post Explain this program please

    Code:
    #include<stdio.h>
    int main(void)
    {
    	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("\w=%d x=%d y=%d z=%d",w,x,y,z);
    }
    I was implementing this code in Pelles C. Please explain how this works.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    If you don't understand it, why were you implementing it?

    How about you try to explain it to us, and we'll tell you if you're correct.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    "Hi, I got this homework/test question and I don't feel like thinking about it. Please provide me with the answer."

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    I don't feel like thinking about your homework either. I don't get the grade, you do.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    #include<stdio.h>    // preprocessor directive
    
    int main(void)    // "main()" function
    {
        int i=4,j=-1,k=0,w,x,y,z;    // variable declarations
        w=i||j||k;    // define value for 'w'
        x=i&&j&& k;   // define value for 'x'
        y=i||j&&amp;k;    // define value for 'y'
        z=i&&j||k;    // define value for 'z'
        printf("\w=%d x=%d y=%d z=%d",w,x,y,z);  // prints the values of 'w', 'x',
                                                 // 'y', and 'z' to the screen
        // what, no return value?
    }

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Terrible Matticus! You didn't line up your comments nice and neatly!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    I'm intrigued now! I might just compile it and see what happens!

    (seriously, I have no idea if you can use logic operators in variable assignments! EDIT: It seems as though you can. Interesting...)
    Last edited by The Doctor; 09-18-2012 at 04:55 PM.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    How this works is that you search the web for a programming forum, then sign up without reading the rules of the site and often without reading the FAQ, then you possibly post code and expect the contributors to tell you everything you need to know about it. Usually this is without code tags, though we are obviously sometimes lucky there. Then the contributors get annoyed at you for being too stupid to not just search for the exact code using the very same search engine to begin with, since it is bound to have been asked about a thousand times already, and for being too lazy to look up how the various language constructs themselves work. such that you might have been able to solve the problem on your own, or at least post a good attempt at understanding it, and learn it better. Then you get berrated for the above, and either lash out at those who point out the shortcomings of your approach to getting help, or you simply disappear and don't come back. Or sometimes you get told about this article, which you read and then understand the problem. Or in some cases you even actually realise how you messed up your approach to getting help initially, then change your ways and stick around and eventualy learn something.

    Yeah that mostly sums it up, glad I could help!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Think about how

    Code:
    while( 1 ){ }
    and

    Code:
    while( j || k || r ){     }
    are both valid expressions. Also think about how the second expression evaluates inside the test expression.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-23-2011, 06:27 AM
  2. Can someone explain this program to me?
    By jaja009 in forum C Programming
    Replies: 4
    Last Post: 03-28-2010, 03:10 PM
  3. Could someone explain this program?
    By Beachblue in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:51 AM
  4. Explain the output of the C program
    By abacus00 in forum C Programming
    Replies: 0
    Last Post: 03-24-2003, 06:24 PM
  5. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 09:53 AM

Tags for this Thread