Re: Any exit status without explicitely using return /exit

Discussion in 'C Programming' started by Keith Thompson, Feb 24, 2010.

  1. Debanjan <> writes:
    > This actually bugging me from quite sometime now.The question is like
    > this : How to set the the exit status of a program to any value
    > without explicitly using return/exit in gcc ?

    [...]

    Some equally interesting questions:

    How can you add two numbers without using the "+" operator?

    How can you call a function without using a function call?

    How can you pound a nail without using a hammer?

    A more relevant question: Why don't you want to use return or exit?
    That's what they're for.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 24, 2010
    #1
    1. Advertising

  2. Keith Thompson

    santosh Guest

    Debanjan <> writes:
    [ ... ]

    > For the question I asked,I modified it a bit
    >
    > int f(int n){
    > return (n>0);
    > }
    >
    > int main(){
    > int n;
    > while(scanf("%d",&n)&&f(n))
    > printf("%d\n",n);
    > }
    >
    > Now it returns 0 implicitly,but I am unable to draw any firm
    > conclusion from this,could anybody help to generalize ?


    If you're thinking the call to f() returns 0 from main() implicitly,
    then you're not correct. As Jacob said, under C99 omitting an
    explicit return from main() returns zero, but not your call to f().
    The call to f() can be replaced with n > 0 in the loop condition. And
    the problems Ike pointed out still remain.

    Don't worry about exit statuses in obfuscated C contests. For
    learning C, ignore obfuscated C contests, and just use return/exit.
    santosh, Feb 24, 2010
    #2
    1. Advertising

  3. Keith Thompson

    Alan Curry Guest

    In article <>,
    Debanjan <> wrote:
    |
    |int f(int n){
    | return (n>0);
    | }
    |
    |int main(){
    | int n;
    | while(scanf("%d",&n)&&f(n))
    | printf("%d\n",n);
    | }
    |
    |Now it returns 0 implicitly,but I am unable to draw any firm
    |conclusion from this,could anybody help to generalize ?

    Generally the return value you'll get from that program will be whatever
    value happened to be left over in the integer return register when the
    function reached its end. So it might even vary randomly between runs of the
    same compiler, even without changing anything. Don't do that.

    If you compile in C99 mode (gcc -std=c99) it'll do the implicit return 0 from
    the end of main, consistently.

    One of the stupider C99 features, it's not provided unless you ask for it.

    --
    Alan Curry
    Alan Curry, Feb 24, 2010
    #3
  4. (Alan Curry) writes:
    [...]
    > Generally the return value you'll get from that program will be whatever
    > value happened to be left over in the integer return register when the
    > function reached its end. So it might even vary randomly between runs of the
    > same compiler, even without changing anything. Don't do that.


    Generally the return value will be garbage (assuming a C90
    implementation). On some specific implementations that garbage
    might happen to be "whatever value happened to be left over in the
    integer return register when the function reached its end".

    > If you compile in C99 mode (gcc -std=c99) it'll do the implicit
    > return 0 from the end of main, consistently.
    >
    > One of the stupider C99 features, it's not provided unless you ask for it.


    gcc doesn't provide it unless you ask for it. Other implementations
    behave differently.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 24, 2010
    #4
  5. Keith Thompson

    Richard Bos Guest

    Debanjan <> wrote:

    > On Feb 24, 2:02=A0pm, Keith Thompson <> wrote:
    > > Debanjan <> writes:
    > > > This actually bugging me from quite sometime now.The question is like
    > > > this : How to set the the exit status of a program to any value
    > > > without explicitly using return/exit in gcc ?


    > > A more relevant question: Why don't you want to use return or exit?
    > > That's what they're for.

    >
    > There are few online coding challenge contest where less the number of
    > character more the points you earned


    That is an _extremely_ bad idea. Don't do it. Write for legibility
    instead.

    Richard
    Richard Bos, Feb 24, 2010
    #5
  6. (Richard Bos) writes:
    > Debanjan <> wrote:
    >> On Feb 24, 2:02=A0pm, Keith Thompson <> wrote:
    >> > Debanjan <> writes:
    >> > > This actually bugging me from quite sometime now.The question is like
    >> > > this : How to set the the exit status of a program to any value
    >> > > without explicitly using return/exit in gcc ?

    >
    >> > A more relevant question: Why don't you want to use return or exit?
    >> > That's what they're for.

    >>
    >> There are few online coding challenge contest where less the number of
    >> character more the points you earned

    >
    > That is an _extremely_ bad idea. Don't do it. Write for legibility
    > instead.


    Agreed. First, learn how to write *clear* code. Once you've done
    that, contests where you try to minimize the number of source
    characters can be entertaining (I've entered the IOCCC myself), but
    you could be learning some very bad habits.

    In this particular instance, there is *no* good reason to avoid the
    use of exit and return. (You can get away with it if you have a C99
    implementation and the status you want to return happens to be 0, but
    even then I'd say that an explicit "return 0;" is a good idea.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 24, 2010
    #6
  7. Keith Thompson

    Seebs Guest

    On 2010-02-24, Debanjan <> wrote:
    > Now it returns 0 implicitly,but I am unable to draw any firm
    > conclusion from this,could anybody help to generalize ?


    Yes:

    Sometimes what happens when you invoke undefined behavior may be surprising,
    unpredictable, or influenced by things which don't matter.

    Seriously, anything you try to draw from this will be wrong on other systems,
    or on the same system in a different phase of the moon. You're thinking about
    this in a totally wrong way. The correct generalization is that, assuming
    a C89 environment, the return value is undefined, and it could be anything.
    It may appear to exhibit patterns. It may even exhibit patterns on some
    systems, some of the time, at some optimization levels.

    But since the code is wrong, who cares what it does?

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Feb 24, 2010
    #7
  8. Seebs <> writes:
    > On 2010-02-24, Debanjan <> wrote:
    >> Now it returns 0 implicitly,but I am unable to draw any firm
    >> conclusion from this,could anybody help to generalize ?

    >
    > Yes:
    >
    > Sometimes what happens when you invoke undefined behavior may be surprising,
    > unpredictable, or influenced by things which don't matter.
    >
    > Seriously, anything you try to draw from this will be wrong on other
    > systems, or on the same system in a different phase of the moon.
    > You're thinking about this in a totally wrong way. The correct
    > generalization is that, assuming a C89 environment, the return value
    > is undefined, and it could be anything. It may appear to exhibit
    > patterns. It may even exhibit patterns on some systems, some of the
    > time, at some optimization levels.


    And those patterns can be interesting, or even useful,
    in understanding how a specific implementation does things.
    In particular, understanding something about the inner workings of
    a specific implementation can be useful for tracking down bugs.

    For example, if I print the value of an integer object and it
    appears to be garbage, that tells me there's a bug somewhere.
    If I happen to recognize that the garbage value looks like a heap
    address or a floating-point representation, it tells me something
    about what the bug is and how to fix it.

    But the goal should (almost) always be to use this information
    to fix the bug, so the program's behavior no longer depends on
    implementation-specific features.

    > But since the code is wrong, who cares what it does?


    In this particular case, the bug is that the program fails to return
    a value to the environment. That's easy to spot and easy to fix:
    add a "return 0;" statement (or use a C99 implementation, but even
    then the "return 0;" statement isn't a bad idea).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 24, 2010
    #8
  9. Keith Thompson

    Alan Curry Guest

    In article <>,
    Keith Thompson <> wrote:
    | (Alan Curry) writes:
    |[...]
    |> Generally the return value you'll get from that program will be whatever
    |> value happened to be left over in the integer return register when the
    |> function reached its end. So it might even vary randomly between runs of the
    |> same compiler, even without changing anything. Don't do that.
    |
    |Generally the return value will be garbage (assuming a C90
    |implementation). On some specific implementations that garbage
    |might happen to be "whatever value happened to be left over in the
    |integer return register when the function reached its end".
    |
    |> If you compile in C99 mode (gcc -std=c99) it'll do the implicit
    |> return 0 from the end of main, consistently.
    |>
    |> One of the stupider C99 features, it's not provided unless you ask for it.
    |
    |gcc doesn't provide it unless you ask for it. Other implementations
    |behave differently.

    Gee I though since the thread was started with explicit mention of gcc, I
    might provide an answer based on what gcc is actually doing, and how its
    behavior can be controlled.

    But direct answers to questions actually asked never go unpunished on
    comp.lang.c

    --
    Alan Curry
    Alan Curry, Feb 25, 2010
    #9
  10. (Alan Curry) writes:
    > In article <>,
    > Keith Thompson <> wrote:
    > | (Alan Curry) writes:

    [...]
    > |> If you compile in C99 mode (gcc -std=c99) it'll do the implicit
    > |> return 0 from the end of main, consistently.
    > |>
    > |> One of the stupider C99 features, it's not provided unless you ask for it.
    > |
    > |gcc doesn't provide it unless you ask for it. Other implementations
    > |behave differently.
    >
    > Gee I though since the thread was started with explicit mention of gcc, I
    > might provide an answer based on what gcc is actually doing, and how its
    > behavior can be controlled.
    >
    > But direct answers to questions actually asked never go unpunished on
    > comp.lang.c


    You feel punished? You must have a thin skin.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 25, 2010
    #10
  11. Keith Thompson

    Tim Rentsch Guest

    Keith Thompson <> writes:

    > Debanjan <> writes:
    >> This actually bugging me from quite sometime now.The question is like
    >> this : How to set the the exit status of a program to any value
    >> without explicitly using return/exit in gcc ?

    > [...]
    >
    > Some equally interesting questions:
    >
    > How can you add two numbers without using the "+" operator?
    >
    > How can you call a function without using a function call?
    >
    > How can you pound a nail without using a hammer?
    >
    > A more relevant question: Why don't you want to use return or exit?
    > That's what they're for.


    I'm sure it wasn't meant to sound this way, but as I read it this
    response seemed rather harsh. The question seems to me to be a
    fair question, perhaps even somewhat interesting. So even though
    I probably agree with the implicit advice in the Socratic last
    question, I hope it's reasonable to suggest giving friendlier and
    more helpful answers, even in cases where the questioner may be
    motivated by concerns that are stylistically suboptimal.
    Tim Rentsch, Mar 3, 2010
    #11
    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. Amelyan
    Replies:
    0
    Views:
    374
    Amelyan
    May 24, 2005
  2. Phaedrus

    Explicitely calling Thread.run

    Phaedrus, Jul 5, 2003, in forum: Java
    Replies:
    3
    Views:
    460
    Roedy Green
    Jul 6, 2003
  3. Victor Bazarov

    Calling constructor explicitely

    Victor Bazarov, Feb 23, 2005, in forum: C++
    Replies:
    9
    Views:
    4,453
    Ron Natalie
    Feb 24, 2005
  4. jacob navia
    Replies:
    3
    Views:
    542
    Nick Keighley
    Feb 24, 2010
  5. kaleem
    Replies:
    8
    Views:
    785
    Tad McClellan
    Dec 14, 2006
Loading...

Share This Page