112

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

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
Kraken
  • 23,393
  • 37
  • 102
  • 162
  • gee ... please keep your code well formatted. – Jay Zhu Mar 11 '10 at 13:22
  • 9
    If you want answers, format your code and question so people can read it. AND DON'T SHOUT! –  Mar 11 '10 at 13:22
  • I do not get what you are saying because i simply cannot read this ugly block of uppercase letters. Write clear sentences, remove your caps-lock key and we might actually _want_ to help you. –  Mar 11 '10 at 13:24
  • 4
    Code format is one thing, but this is written poorly. You don't want answers that look like this, do you? – Kobi Mar 11 '10 at 13:24
  • 1
    man the code format is the same as i read it frm the book... nd if u want me to remove the caps i can do that.. wait.. – Kraken Mar 11 '10 at 13:28
  • yeah...u already did that.. thnx – Kraken Mar 11 '10 at 13:29
  • 57
    oy! gotos and exit? My eyes! They burn! – Kyle Mar 11 '10 at 13:33
  • 8
    I'm no C-Expert nor can I write it without difficulty so I may be wrong about this...but may I suggest that you throw that book away? Using goto in C is like...like...I don't know. And if you excuse me now, they figured out how to open doors \*hides.in.the.kitchen\*... – Bobby Mar 11 '10 at 13:35
  • 9
    Well, goto still is valid syntax in C. It may be disapproved _stylistically_ , but it is still valid. `void main()` OTOH is just **plain wrong**. Just as `exit();` is wrong, and omitting `#include ` is wrong. – wildplasser Jun 23 '13 at 15:50
  • 1
    Also, "Go To Statement Considered Harmful" was published _more than *45* years ago_. Take it with a grain of salt. – geometrian Feb 16 '14 at 04:02
  • 1
    I have no problem with goto. I mean, I'd never put it in something that I had to give to someone else (if I were, for example, programming at a job), but that's only because some people incorrectly believe that the world will end if I use a goto - NOT because they are a terrible evil. I use gotos all the time in my personal programs (e.g. ones only I will ever see). – Wyatt Ward Aug 10 '14 at 23:17
  • @wildplasser: I share your dislike of `void main()`, but it's not *quite* just **plain wrong**. Implementations are permitted to support additional implementation-defined definitions for `main`, and some specifically document their support for `void main()` (or at least for `void main(void)`; I haven't checked). On the other hand, there is no good reason not to use one of the standard forms (unless you're using a freestanding (i.e., embedded) implementation). – Keith Thompson Nov 26 '14 at 18:23
  • @KeithThompson: I know about embedded ("freestanding") platforms, but exit() does not seem very useful there. [and even if: exit() without an argument could be worse than dropping out of void main() ... ] – wildplasser Nov 26 '14 at 18:55
  • 1
    @wildplasser: It depends on the system. A freestanding implementation *could* support some of the C standard library. On the other hand, if there's no operating system and your program is the only thing running on the box, there's nothing to `exit` to. – Keith Thompson Nov 26 '14 at 18:58
  • That's what I meant: there is nothing to exit to. So this *must* be a hosted implementation. (which is *allowed* to use void main(), but there is no good reason for this.) – wildplasser Nov 26 '14 at 19:03
  • @wildplasser: It's an interesting question whether an implementation for a system with an OS can be called "freestanding". The standard (N1570 5.1.2) refers to a freestanding implementation as one "in which C program execution may take place without any benefit of an operating system". The word "may" could leave some wiggle room; I might even call an implementation "freestanding" because I was too lazy to implement the full standard library. Furthermore, "The effect of program termination in a freestanding environment is implementation- defined." (Pedant, you say? Why yes!) – Keith Thompson Nov 26 '14 at 22:49
  • @KeithThompson Is that theoretically possible? A program running on top of hardware, without an O.S. layer in between? Wouldn't the program then be the O.S.? – entitycs Sep 15 '15 at 18:40
  • @DustinCharles: Yes, the program would effectively be the OS -- but it wouldn't necessarily have the features you normally associate with an OS (like running other programs). Consider an embedded system that controls hardware. The CPU does one and only one thing. You've probably got something like that in your keyboard, for example. – Keith Thompson Sep 15 '15 at 19:02

13 Answers13

182

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • 7
    The [man page](http://man7.org/linux/man-pages/man3/exit.3.html) says "` The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-UNIX environments) than the use of 0 and some nonzero value like 1 or -1. `" – Mawg says reinstate Monica May 16 '19 at 08:35
88

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

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
  • 4
    +1 for not only mentioning that `exit` takes an integer argument, but explaining why it takes an integer argument and that some compilers require the explicit `#include `. – Variadicism Oct 09 '15 at 18:00
  • The mentioned predefined macros are defined in compilation time, aren't they? – carloswm85 Aug 10 '18 at 17:12
15

Try man exit.


Oh, and:

#include <stdlib.h>

int main(void) {
  /*  ...  */
  if (error_occured) {
    return (EXIT_FAILURE);
  }
  /*  ...  */
  return (EXIT_SUCCESS);
}
Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
14

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[])
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • yeah its written in the book that u better sont use goto bt for the sake of completeness of the book m just giving u an example... so the book aint that bad!! – Kraken Mar 11 '10 at 13:33
  • 6
    `goto` is occasionally the right thing to do (and the Linux kernel provides many examples of that, but it's not the only code that can use it). But OP's example is clearly not one of those cases, so yes, that book should be burned :). – Adam Rosenfield Mar 24 '11 at 00:56
9

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nataraj Raja
  • 117
  • 1
  • 6
4

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

slayer
  • 214
  • 4
  • 11
Martin max
  • 59
  • 1
3

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.

ultimate cause
  • 2,264
  • 4
  • 27
  • 44
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

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.

filvarga
  • 56
  • 3
-2

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
-3

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.

Gaurav
  • 1
  • 1
  • 1
    `` is specific to C++, and is not related to ``, the header that's actually needed. The only portable arguments to the `exit` function are `0`, `EXIT_SUCCESS`, and `EXIT_FAILURE`. The use of any non-zero value to denote failure is specific to UNIX-like systems (and probably some others); don't rely on it in portable code. – Keith Thompson Nov 26 '14 at 18:20
-4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 6
    `abort` is **not** the appropriate thing to call here, it won't call `atexit` handlers or flush open file buffers. It should only be used for abnormal program termination, e.g. failed assertions etc. For normal program termination, `exit` should be used. – Adam Rosenfield Mar 24 '11 at 00:58
-6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Detamos
  • 1
  • 1
  • 4
    I don't know what process.h is; it's certainly not standard C, and will not work on systems that don't provide it. The `` header is specific to C++; the question is about C. – Keith Thompson Nov 26 '14 at 18:18
  • 1
    This isn't Yahoo! Answers, it's Stack Overflow. If you answer, you need to have at least tested it out first! – wizzwizz4 Dec 26 '15 at 19:27