Use of exit() function

后端 未结 13 1391
离开以前
离开以前 2020-11-30 23:34

I want to know how and when can I use the exit() function like the program in my book:

#include

void main()
{
    int goals;
            


        
相关标签:
13条回答
  • 2020-11-30 23:41

    Try using exit(0); instead. The exit function expects an integer parameter. And don't forget to #include <stdlib.h>.

    0 讨论(0)
  • 2020-11-30 23:47

    You must add a line with #include <stdlib.h> to include that header file and exit must return a value so assign some integer in exit(any_integer).

    0 讨论(0)
  • 2020-11-30 23:48

    exit(int code); is declared in stdlib.h so you need an

    #include <stdlib.h>
    

    Also:
    - You have no parameter for the exit(), it requires an int so provide one.
    - Burn this book, it uses goto which is (for everyone but linux kernel hackers) bad, very, very, VERY bad.

    Edit:
    Oh, and

    void main()
    

    is bad, too, it's:

    int main(int argc, char *argv[])
    
    0 讨论(0)
  • 2020-11-30 23:49

    The exit() function is a type of function with a return type without an argument. It's defined by the stdlib header file.

    You need to use ( exit(0) or exit(EXIT_SUCCESS)) or (exit(non-zero) or exit(EXIT_FAILURE) ).

    0 讨论(0)
  • 2020-11-30 23:49

    Include stdlib.h in your header, and then call abort(); in any place you want to exit your program. Like this:

    switch(varName)
    {
        case 1: 
         blah blah;
        case 2:
         blah blah;
        case 3:
         abort();
    }
    

    When the user enters the switch accepts this and give it to the case 3 where you call the abort function. It will exit your screen immediately after hitting enter key.

    0 讨论(0)
  • 2020-11-30 23:50

    Try man exit.


    Oh, and:

    #include <stdlib.h>
    
    int main(void) {
      /*  ...  */
      if (error_occured) {
        return (EXIT_FAILURE);
      }
      /*  ...  */
      return (EXIT_SUCCESS);
    }
    
    0 讨论(0)
提交回复
热议问题