I’m Outta Here

A specific command to terminate a program was popular with programming languages long ago. For example, in BASIC you had the END command, which exited a program. For unusual circumstances, you could use the STOP command. It was the abnormal termination command, which stopped the program and also generated a “Break in line” message (in GW Basic). I’m sure other early languages also had commands to bail out of a program, and modern languages are the same.

The C language lacks a keyword to end a program. Most users today would believe that the return keyword does so, but this thought isn’t exactly true.

In the original C Programming Language (K&R) book, the Hello, world! program lacks a return statement. That’s because declaring the main() function as an int wasn’t required until later in the C standard. In fact, many early programs used the void data type for main(), which cemented that it didn’t need a return statement; the program stopped after the last statement in the function was executed. Today it’s different.

You can quit any program by placing a return statement in the main() function. So that’s one way to leave, but doing so works only in the main() function.

When you must bail out from another function, say somewhere deep in the bowels of complex code, use the exit() function. It’s prototyped in the stdlib.h header file and accepts an integer argument, which is the same value a return statement passes to the operating system when a program quits.

The exit() function has a benefit in that it performs a few clean-up chores before it releases the program’s life essence: It calls any functions specified by the atexit() function; it flushes all output streams (sending output to a file or standard output); closes all open streams; and it removes any temporary files created. Especially for larger, complex programs, using exit() is a wise choice.

When you must abandon your program for some hot reason, a good choice is to use the abort() function. Prototyped in the stdlib.h header file (same as exit()), the abort() function it requires no argument: Calling it generates an abnormal program termination condition. It does, however, flush and close any open streams.

The following code demonstrates how effective and rude the abort() function can be:

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

int main()
{
    char c;

    printf("Type a letter: ");
    scanf("%c",&c);
    if( c > 'M' )
        abort();
    puts("Whew!");

    return(0);
}

The code prompts for input and compares the letter typed with M. If the character is greater (all the lowercase letters are), the program aborts. Otherwise, the code displays “Whew!” and terminates normally.

Here’s a sample run:
Type a letter: A
Whew!

And another:
Type a letter: i
Abort trap 6

On my Ubuntu system, the error message is Aborted (core dumped). On the PC in Code::Blocks, the program stalled for a few seconds and then terminated without any supplemental message. Regardless, the point is made: You’re program has exited abruptly. This type of message can be handy for you as a programmer, though I would caution against using it casually in that such an abrupt exit requires more explanation for a user.

Leave a Reply