Thread: Please explain why output is 4??

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20

    Please explain why output is 4??

    Code:
    //Compiled on gcc
    //I know that this is dependent on compiler but how we are getting o/p as 4 on gcc
    #include<stdio.h>
    int main()
    {
        int i=1,j=2,k;
        k=(i--,j--) + j-- ;
        printf("%d",i);
    }
    O/P:
    4

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ackr1201 View Post
    //I know that this is dependent on compiler but how we are getting o/p as 4 on gcc
    Luck.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Same reason you were given by Laser

    Read me this time.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > //I know that this is dependent on compiler but how we are getting o/p as 4 on gcc
    Do gcc -S prog.c, then look through prog.s to see where all the mov/sub/dec instructions are.

    Or learn to stop worrying about how UB 'works' for each and every compiler you might come across
    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.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Since the code invokes undefined behaviour (modifying j twice between two sequence points) the compiler is allowed to do anything.

    My guess is that the value being output is the 42nd byte of the output executable file. Why the 42nd byte? To honour the "Hitchhiker's Guide to the Galaxy". Why does it have the value 4? That is the first digit of 42. Since any explanation what what you see is possible when you invoke undefined behaviour, this is as feasible an explanation as any.

    Better yet, don't bother seeking explanations for particular cases of undefined behaviour. Spend your time learning how to AVOID invoking undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by grumpy View Post
    ... To honour the "Hitchhiker's Guide to the Galaxy". Why does it have the value 4?.......

    Um....Zooey Deschanel in those shorts.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Salem
    To expand on earlier posts, I'm reposting something I wrote some time ago for another forum....
    *cough* plug *cough*
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explain the output ?????
    By shashwat in forum C Programming
    Replies: 4
    Last Post: 07-09-2011, 06:32 AM
  2. 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
  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