Thread: explain the output ?????

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    Question explain the output ?????

    Q) Find the output of:

    Code:
     
    void main()
            {
            int k=35;
            printf("%d %d %d",k==35,k=50,k>40);
            }
    OUTPUT IS:
    0 50 0

    but why not the output is 1 50 0 (as 35==35)

    second is:

    Code:
     
    void main()
            { 
            intx=15;
            printf("%d %d %d",x!=15,x=20,x<30);
            }
    OOUTPUT IS:
    1 20 1

    but according to me output should be 0 20 1 (asx!=15 i.e. 15!=15 is false i.e. 0) why is it not so.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The arguments to a function, in this case prinf, can be executed in any order. In this case the compiler is choosing to execute third argument, the assignment, first.

    For this reason, you're not allowed to write to and read the same memory location in the same line, except if the value read is used to compute the new value written. There are exceptions, but they don't apply here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    Explain the output???

    If you give only the first one it gives the correct result.

    Code:
    #include <stdio.h>
    void main()
    {
          int k=35;
          printf("%d",k==35);
    }
    This gives the result as you expect.

    Go through the following code,

    Code:
    #include <stdio.h>
    void main()
    {
          int k=35;
          printf("%d");
    }
    This will print '35'. So '%d' gets value previously stored.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, as a side thing, the correct main definition is int main(void) or int main(int argc, char *argv[]) not void main(). Read the FAQ.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what will be the output and plz explain me
    By alekhya k in forum C Programming
    Replies: 6
    Last Post: 06-22-2011, 09:48 AM
  2. what is the program output please explain
    By sunil17 in forum C Programming
    Replies: 2
    Last Post: 09-02-2010, 08:34 AM
  3. help: please explain the output
    By soniclavier in forum C Programming
    Replies: 8
    Last Post: 12-07-2009, 12:35 PM
  4. Input & Output Explain?
    By 98dodgeneondohc in forum C Programming
    Replies: 5
    Last Post: 04-24-2005, 06:13 PM
  5. Explain the output of the C program
    By abacus00 in forum C Programming
    Replies: 0
    Last Post: 03-24-2003, 06:24 PM

Tags for this Thread