Does this program have well-defined behaviour?

Discussion in 'C Programming' started by eq mail, Aug 12, 2012.

  1. eq mail

    eq mail Guest

    Does the following have well-defined behaviour?

    #include <stdlib.h>

    int e(void)
    {
    exit(0);
    }

    int main(void)
    {
    return e();
    }
     
    eq mail, Aug 12, 2012
    #1
    1. Advertising

  2. eq mail <> writes:
    > Does the following have well-defined behaviour?
    >
    > #include <stdlib.h>
    >
    > int e(void)
    > {
    > exit(0);
    > }
    >
    > int main(void)
    > {
    > return e();
    > }


    I believe so.

    N1570 6.9.1p12:

    If the } that terminates a function is reached, and the value of the
    function call is used by the caller, the behavior is undefined.

    Execution never reaches the closing } of the function "e".

    That by itself doesn't prove that the behavior is well-defined, but I
    think that everything the program does during execution has well-defined
    behavior.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 13, 2012
    #2
    1. Advertising

  3. eq mail

    Eric Sosman Guest

    On 8/12/2012 6:51 PM, eq mail wrote:
    > Does the following have well-defined behaviour?
    >
    > #include <stdlib.h>
    >
    > int e(void)
    > {
    > exit(0);
    > }
    >
    > int main(void)
    > {
    > return e();
    > }


    Yes, as far as I can see. What is there about it that
    causes you to ask the question? (Character sets, maybe? Are
    you worried that what looks like an x in E-mail might look
    like something else to a C compiler? Can't help you with that
    one -- but if "Things are [not so] seldom what they seem,"
    then I see no problem.)

    --
    Eric Sosman
    d
     
    Eric Sosman, Aug 13, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    6
    Views:
    5,546
    Kevin Spencer
    Aug 18, 2005
  2. Tom Zych

    Is this well-defined?

    Tom Zych, Sep 11, 2003, in forum: C Programming
    Replies:
    16
    Views:
    601
    Peter Nilsson
    Sep 12, 2003
  3. M Welinder

    C99: Is "INT_MIN % -1" well defined?

    M Welinder, Aug 5, 2004, in forum: C Programming
    Replies:
    6
    Views:
    368
    Dik T. Winter
    Aug 7, 2004
  4. Oodini
    Replies:
    1
    Views:
    1,836
    Keith Thompson
    Sep 27, 2005
  5. Jim Langston
    Replies:
    10
    Views:
    841
    Frederick Gotham
    Nov 22, 2006
Loading...

Share This Page