Quiting a program using pure C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kimimaro

    #1

    Quiting a program using pure C

    Is there anymore methods in exiting your program using pure C language
    other than return 0?

  • Nicolas Pavlidis

    #2
    Re: Quiting a program using pure C

    "kimimaro" <little_cloudie @yahoo.com> writes:
    [color=blue]
    > Is there anymore methods in exiting your program using pure C language
    > other than return 0?[/color]

    There is a way:
    exit(0);

    This function, I think it is deklared in stdlib.h finishes your program,
    but be aware of memoryleaks.

    Kind regards,
    Nicolas

    --
    | Nicolas Pavlidis | Elvis Presly: |\ |__ |
    | Student of SE & KM | "Into the goto" | \|__| |
    | pavnic@sbox.tug raz.at | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|

    Comment

    • Ravi Uday

      #3
      Re: Quiting a program using pure C


      "kimimaro" <little_cloudie @yahoo.com> wrote in message
      news:ca796b76d9 0c60e1d813cf954 9122799@localho st.talkaboutpro gramming.com...[color=blue]
      > Is there anymore methods in exiting your program using pure C language
      > other than return 0?
      >[/color]

      you can also have
      return EXIT_FAILURE;
      return EXIT_SUCCESS;

      as defined in <stdlib.h>

      - Ravi


      Comment

      • Method Man

        #4
        Re: Quiting a program using pure C


        "Nicolas Pavlidis" <pavnic@sbox.tu graz.at> wrote in message
        news:2v8v0pF2he qtgU3@uni-berlin.de...[color=blue]
        > "kimimaro" <little_cloudie @yahoo.com> writes:
        >[color=green]
        > > Is there anymore methods in exiting your program using pure C language
        > > other than return 0?[/color]
        >
        > There is a way:
        > exit(0);
        >
        > This function, I think it is deklared in stdlib.h finishes your program,
        > but be aware of memoryleaks.
        >[/color]

        Most* OS's free the memory when a program has exited so it's not really a
        concern over memory leaks, but rather on programming style.

        *All that I know of.


        Comment

        • Michael Mair

          #5
          Re: Quiting a program using pure C



          kimimaro wrote:[color=blue]
          > Is there anymore methods in exiting your program using pure C language
          > other than return 0?[/color]

          Yep, #include <stdlib.h> and use exit() and abort() (actually,
          don't use abort()...):
          Use
          exit(0);
          exit(EXIT_SUCCE SS);
          if your program is terminated due to circumstances you think
          okay (job done, help message printed, ...) and
          exit(EXIT_FAILU RE);
          if something is definitely not okay.

          If you want some things done when exiting your program, have
          a look at atexit().

          Note: Not all environments where you can run programs clean up
          the memory, so you should take care of it before calling exit().


          Cheers
          Michael
          --
          E-Mail: Mine is a gmx dot de address.

          Comment

          • bd

            #6
            Re: Quiting a program using pure C

            kimimaro wrote:
            [color=blue]
            > Is there anymore methods in exiting your program using pure C language
            > other than return 0?[/color]

            You can use the exit() or abort() functions.

            Comment

            • pete

              #7
              Re: Quiting a program using pure C

              kimimaro wrote:[color=blue]
              >
              > Is there anymore methods in exiting your program using pure C language
              > other than return 0?[/color]

              Assuming that "by pure C" you're not making the distiction
              between the c language proper and the library:
              There's the assert macro in <assert.h>

              --
              pete

              Comment

              • Edmund Bacon

                #8
                Re: Quiting a program using pure C

                bd <bdonlan@gmail. com> writes:
                [color=blue]
                > kimimaro wrote:
                >[color=green]
                > > Is there anymore methods in exiting your program using pure C language
                > > other than return 0?[/color]
                >
                > You can use the exit() or abort() functions.[/color]

                And don't forget: assert(0);

                Comment

                • Michael Mair

                  #9
                  Re: Quiting a program using pure C


                  Edmund Bacon wrote:[color=blue]
                  > bd <bdonlan@gmail. com> writes:
                  >[color=green]
                  >>kimimaro wrote:
                  >>[color=darkred]
                  >>>Is there anymore methods in exiting your program using pure C language
                  >>>other than return 0?[/color]
                  >>
                  >>You can use the exit() or abort() functions.[/color]
                  >
                  > And don't forget: assert(0);[/color]

                  Note: The assert()-macro, if "called" with an expression evaluating
                  to zero, prints an error message and calls abort().

                  -Michael
                  --
                  E-Mail: Mine is a gmx dot de address.

                  Comment

                  • Jack Klein

                    #10
                    Re: Quiting a program using pure C

                    On Mon, 8 Nov 2004 05:21:11 -0500, "Method Man" <a@b.c> wrote in
                    comp.lang.c:
                    [color=blue]
                    >
                    > "Nicolas Pavlidis" <pavnic@sbox.tu graz.at> wrote in message
                    > news:2v8v0pF2he qtgU3@uni-berlin.de...[color=green]
                    > > "kimimaro" <little_cloudie @yahoo.com> writes:
                    > >[color=darkred]
                    > > > Is there anymore methods in exiting your program using pure C language
                    > > > other than return 0?[/color]
                    > >
                    > > There is a way:
                    > > exit(0);
                    > >
                    > > This function, I think it is deklared in stdlib.h finishes your program,
                    > > but be aware of memoryleaks.
                    > >[/color]
                    >
                    > Most* OS's free the memory when a program has exited so it's not really a
                    > concern over memory leaks, but rather on programming style.
                    >
                    > *All that I know of.[/color]

                    There have been, and still are, a few that don't, or don't always.

                    The C standard cannot and does not place any requirements on what a
                    platform might do before or after the execution of a C program. So
                    the C standard neither requires nor guarantees that an operating
                    system is able to clean up memory.

                    --
                    Jack Klein
                    Home: http://JK-Technology.Com
                    FAQs for
                    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                    alt.comp.lang.l earn.c-c++

                    Comment

                    • ranjeet

                      #11
                      Re: Quiting a program using pure C

                      Michael Mair <Michael.Mair@i nvalid.invalid> wrote in message news:<2v9efbF2i iv2mU1@uni-berlin.de>...[color=blue]
                      > Edmund Bacon wrote:[color=green]
                      > > bd <bdonlan@gmail. com> writes:
                      > >[color=darkred]
                      > >>kimimaro wrote:
                      > >>
                      > >>>Is there anymore methods in exiting your program using pure C language
                      > >>>other than return 0?
                      > >>
                      > >>You can use the exit() or abort() functions.[/color]
                      > >
                      > > And don't forget: assert(0);[/color]
                      >
                      > Note: The assert()-macro, if "called" with an expression evaluating
                      > to zero, prints an error message and calls abort().[/color]

                      NOTE : what I think is that when we call assert
                      And if it get the agrumnet as NULL then
                      Its display the message as we see in the case of the Poiters
                      when it gets the Zero as an argument. then it Returns the 0
                      (Exit the program)

                      Is my knowledge about assert is correct ????? help in correcting
                      If i am wrong.

                      Regards
                      Ranjeet.
                      [color=blue]
                      > -Michael[/color]

                      Comment

                      • Michael Mair

                        #12
                        Re: Quiting a program using pure C



                        ranjeet wrote:[color=blue]
                        > Michael Mair <Michael.Mair@i nvalid.invalid> wrote in message news:<2v9efbF2i iv2mU1@uni-berlin.de>...
                        >[color=green]
                        >>Edmund Bacon wrote:
                        >>[color=darkred]
                        >>>bd <bdonlan@gmail. com> writes:
                        >>>
                        >>>
                        >>>>kimimaro wrote:
                        >>>>
                        >>>>
                        >>>>>Is there anymore methods in exiting your program using pure C language
                        >>>>>other than return 0?
                        >>>>
                        >>>>You can use the exit() or abort() functions.
                        >>>
                        >>>And don't forget: assert(0);[/color]
                        >>
                        >>Note: The assert()-macro, if "called" with an expression evaluating
                        >>to zero, prints an error message and calls abort().[/color]
                        >
                        >
                        > NOTE : what I think is that when we call assert
                        > And if it get the agrumnet as NULL then
                        > Its display the message as we see in the case of the Poiters
                        > when it gets the Zero as an argument. then it Returns the 0
                        > (Exit the program)
                        >
                        > Is my knowledge about assert is correct ????? help in correcting
                        > If i am wrong.[/color]

                        I am sorry, I do not understand what you want to tell me.
                        Just have a peek at this implementation of assert as my_assert.
                        I just whipped it up but it should fulfill the basic requirements
                        for assert. The first five assertions will fail.
                        Use MY_NDEBUG instead of NDEBUG for switching my_assert off.
                        If this does not answer your question/complement your
                        understanding/whatever feel free to ask again.


                        Cheers
                        Michael

                        -----------------------------------------------------------------
                        #include <stdio.h>
                        #include <stdlib.h>

                        #if defined __STDC_VERSION_ _ && __STDC_VERSION_ _ >= 199901L
                        # define ASSERT_FUNCTION __func__
                        #else
                        # define ASSERT_FUNCTION ((const char *) 0)
                        #endif

                        #define STRINGIZE(expr) # expr
                        #define XSTR(expr) STRINGIZE(expr)

                        #ifdef MY_NDEBUG
                        # define my_assert(expr) ((void) (0))
                        #else
                        # define my_assert(expr) \
                        ((void) ((expr) ? 0 : \
                        (fprintf(stderr , "Assertion \"" XSTR(expr) "\" failed in %s, "\
                        "line %d (function %s).\n", __FILE__, __LINE__, \
                        ASSERT_FUNCTION ? ASSERT_FUNCTION : "?"),abort( ), 0)))
                        #endif /* MY_NDEBUG */


                        int main (int argc, char **argv)
                        {
                        switch (argc) {
                        case 0:
                        fprintf(stdout, "Usage: <program> <assertion>\n "
                        "\tassertio ns: 0-9\n");
                        break;
                        case 1:
                        fprintf(stdout, "Usage: %s <assertion>\n "
                        "\tassertio ns: 0-5\n", argv[0]);
                        break;
                        default:
                        switch (atoi(argv[1])) {
                        case 0:
                        my_assert(0);
                        case 1:
                        my_assert(NULL) ;
                        case 2:
                        my_assert(1 == 2);
                        case 3:
                        my_assert(argv[argc] != NULL);
                        case 4:
                        my_assert(0x2b & ~0x2b);
                        default:
                        my_assert(1);
                        }
                        fprintf(stdout, "arrived here\n\n");
                        }

                        return 0;
                        }

                        --
                        E-Mail: Mine is a gmx dot de address.

                        Comment

                        • Michael Wojcik

                          #13
                          Re: Quiting a program using pure C


                          In article <77c88a3b.04110 90027.60a6641c@ posting.google. com>, ranjeet.gupta@g mail.com (ranjeet) writes:[color=blue]
                          >
                          > NOTE : what I think is that when we call assert[/color]

                          assert is a macro, not a function. It's not called, properly speaking.
                          It's expanded with argument substitution.
                          [color=blue]
                          > And if it get the agrumnet as NULL then[/color]

                          assert evaluates its argument as an integer expression. NULL is a
                          macro that evaluates to a null pointer constant. All assert cares
                          about is the truth value of its argument: assert does nothing if
                          the argument is non-zero, or if the NDEBUG macro was defined (where
                          assert.h was included in the current translation unit); otherwise it
                          writes a message to standard error and calls the abort function.

                          NULL is not the same thing as zero. NULL will evaluate to false, but
                          the argument to the assert macro need not be a pointer value.
                          [color=blue]
                          > Its display the message as we see in the case of the Poiters
                          > when it gets the Zero as an argument. then it Returns the 0
                          > (Exit the program)[/color]

                          No, it calls the abort function. It does not return anything.

                          The abort function will cause the program to terminate unless the
                          SIGABRT signal is being caught and the signal handler does not
                          return. Whether this counts as "exit[ing] the program" is debatable;
                          the program stops running, but not necessarily in the same manner as
                          if the exit function were called. (For example, output streams may
                          not be flushed.)

                          In practice, it's often enough to simply think of assert as "test
                          this condition and kill the program if it's false". The actual
                          situation, however, is more complicated, and if you're writing
                          serious C programs it is probably a good idea to understand it.

                          --
                          Michael Wojcik michael.wojcik@ microfocus.com

                          It wasn't fair; my life was now like everyone else's. -- Eric Severance

                          Comment

                          • Chris Torek

                            #14
                            Re: Quiting a program using pure C

                            In article <news:cmqh6p0ln a@news3.newsguy .com>
                            Michael Wojcik <mwojcik@newsgu y.com> wrote:[color=blue]
                            >assert is a macro, not a function. It's not called, properly speaking.
                            >It's expanded with argument substitution.[/color]

                            [Correct]
                            [color=blue][color=green]
                            >> And if it get the agrumnet as NULL then[/color]
                            >
                            >assert evaluates its argument as an integer expression. NULL is a
                            >macro that evaluates to a null pointer constant. ...[/color]

                            The implication here -- which is also correct -- is that assert(NULL)
                            does not have to compile.

                            This is sort of a bug in C89, and is fixed in C99, where the argument
                            to assert() has type "bool" (from <stdbool.h>; really _Bool). In
                            C99, assert(NULL) is a valid invocation that -- if NDEBUG is not
                            defined -- emits a message and calls abort().
                            [color=blue]
                            >NULL is not the same thing as zero. NULL will evaluate to false, but
                            >the argument to the assert macro need not be a pointer value.[/color]

                            In general, even in C89 implementations , assert(ptr) compiles fine
                            and does the same thing as it is required to do in C99. But a
                            careful C89 programmer really should write:

                            assert(ptr != NULL); /* not just assert(ptr) */

                            "just in case" the program is ever ported to a C89 compiler that
                            is particularly picky.

                            (I think Michael Wojcik knows all of this, but it was not specifically
                            called out in his article.)
                            --
                            In-Real-Life: Chris Torek, Wind River Systems
                            Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                            email: forget about it http://web.torek.net/torek/index.html
                            Reading email is like searching for food in the garbage, thanks to spammers.

                            Comment

                            • Dan Pop

                              #15
                              Re: Quiting a program using pure C

                              In <cmqjqc12r60@ne ws2.newsguy.com > Chris Torek <nospam@torek.n et> writes:
                              [color=blue]
                              >In article <news:cmqh6p0ln a@news3.newsguy .com>
                              >Michael Wojcik <mwojcik@newsgu y.com> wrote:[color=green]
                              >>assert is a macro, not a function. It's not called, properly speaking.
                              >>It's expanded with argument substitution.[/color]
                              >
                              >[Correct]
                              >[color=green][color=darkred]
                              >>> And if it get the agrumnet as NULL then[/color]
                              >>
                              >>assert evaluates its argument as an integer expression. NULL is a
                              >>macro that evaluates to a null pointer constant. ...[/color]
                              >
                              >The implication here -- which is also correct -- is that assert(NULL)
                              >does not have to compile.
                              >
                              >This is sort of a bug in C89, and is fixed in C99, where the argument
                              >to assert() has type "bool" (from <stdbool.h>; really _Bool).[/color]

                              Nope, in C99 the argument to assert has scalar type.
                              [color=blue]
                              >In C99, assert(NULL) is a valid invocation that -- if NDEBUG is not
                              >defined -- emits a message and calls abort().[/color]

                              You're contradicting yourself: if assert expected a bool argument, passing
                              it (void *)0 would be far from correct: undefined behaviour.

                              Dan
                              --
                              Dan Pop
                              DESY Zeuthen, RZ group
                              Email: Dan.Pop@ifh.de
                              Currently looking for a job in the European Union

                              Comment

                              Working...