Use of exit() function

C

C Problem Overview


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

#include<stdio.h>

void main()
{
    int goals;
    printf("enter number of goals scored");
    scanf("%d",&goals);

    if(goals<=5)
        goto sos;
    else
    {
        printf("hehe");
        exit( );
    }
    sos:
    printf("to err is human");
}

When I run it, it shows ERROR: call to undefined function exit().

Also, I want to know how I can create an option to close the window in which the program runs? For example, I made a menu-driven program which had several options and one of them was "exit the menu". How can I make this exit the program (i.e. close the window)?

C Solutions


Solution 1 - C

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

Solution 2 - C

The exit function is declared in the stdlib header, so you need to have

#include <stdlib.h>

at the top of your program to be able to use exit.

Note also that exit takes an integer argument, so you can't call it like exit(), you have to call as exit(0) or exit(42). 0 usually means your program completed successfully, and nonzero values are used as error codes.

There are also predefined macros EXIT_SUCCESS and EXIT_FAILURE, e.g. exit(EXIT_SUCCESS);

Solution 3 - C

Try man exit.


Oh, and:

#include <stdlib.h>

int main(void) {
  /*  ...  */
  if (error_occured) {
    return (EXIT_FAILURE);
  }
  /*  ...  */
  return (EXIT_SUCCESS);
}

Solution 4 - C

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[])

Solution 5 - C

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) ).

Solution 6 - C

The following example shows the usage of the exit() function.

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

int main(void) {
    printf("Start of the program....\n");
    printf("Exiting the program....\n");
    exit(0);
    printf("End of the program....\n");
    return 0;
}

###Output

> Start of the program....
> Exiting the program....

Solution 7 - C

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).

Solution 8 - C

In addition to return an exit code to parent process -

In UNIX, an important aspect that I think has been left out is, that exit() at first calls (in reverse order) all those functions, which were registered by atexit() call.

Please refer to SUSv4 for details.

Solution 9 - C

on unix like operating systems exit belongs to group of system calls. system calls are special calls which enable user code (your code) to call kernel code. so exit call makes some OS specific clean-up actions before returning control to OS, it terminates the program.

#include <stdlib.h>

// example 1
int main(int argc, char *argv){
  exit(EXIT_SUCCESS);
}

// example 2
int main(int argc, char *argv){
  return 0;
}

Some compilers will give you the same opcode from both of these examples but some won't. For example opcode from first function will not include any kind of stack positioning opcode which will be included in the second example like for any other function. You could compile both examples and disassemble them and you will see the difference.

You can use exit from any part of your code and be sure that process terminates. Don't forget to include integer parameter.

Solution 10 - C

Write header file #include<process.h> and replace exit(); with exit(0);. This will definitely work in Turbo C; for other compilers I don't know.

Solution 11 - C

Bad programming practice. Using a goto function is a complete no no in C programming.
Also include header file stdlib.h by writing #include <iostream.h>for using exit() function. Also remember that exit() function takes an integer argument . Use exit(0) if the program completed successfully and exit(-1) or exit function with any non zero value as the argument if the program has error.

Solution 12 - C

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.

Solution 13 - C

Use process.h instead of stdlib and iostream... It will work 100%.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKrakenView Question on Stackoverflow
Solution 1 - CKlaus Byskov PedersenView Answer on Stackoverflow
Solution 2 - CTyler McHenryView Answer on Stackoverflow
Solution 3 - CBertrand MarronView Answer on Stackoverflow
Solution 4 - CMorfildurView Answer on Stackoverflow
Solution 5 - CNataraj RajaView Answer on Stackoverflow
Solution 6 - CMartin maxView Answer on Stackoverflow
Solution 7 - Cuser4508201View Answer on Stackoverflow
Solution 8 - Cultimate causeView Answer on Stackoverflow
Solution 9 - CfilvargaView Answer on Stackoverflow
Solution 10 - CAzeem AlviView Answer on Stackoverflow
Solution 11 - CGauravView Answer on Stackoverflow
Solution 12 - CMuhammad AhmadView Answer on Stackoverflow
Solution 13 - CDetamosView Answer on Stackoverflow