Press "Enter" to skip to content

C program termination

The exit() function is declared in the standard library header file. The exit function is used to exit or terminate the program. The Exit function exits a program while ignoring the rest of the code.
In other words, statements or codes after the exit function declaration are ignored by the system or will not be executed.
EXIT_SUCCESS and EXIT_FAILURE are constants that are defined in header files to indicate exit success or exit failure of programmer code.

How to terminate a running program in c++

to run the xampp control panel. When I run the program after compiling, it runs smoothly, except the program I have written is still running. Once the XAMPP control panel is launched, I want to terminate the program. What could possibly be done? Any help is much much appreciated.

asked May 22, 2015 at 14:57
Romeo Sierra Romeo Sierra
1,636 1 1 gold badge 16 16 silver badges 35 35 bronze badges

The best scenario would be to not use system() to launch it. Instead, launch the new process using any system call that does it without blocking, and then simply exit() your program.

May 22, 2015 at 15:04
Maybe you can use exit (int status); if i understand your problem correctly.
May 22, 2015 at 15:04

Wrong tool for the job: system documentation and relevant quote “and returns after the command has been completed”.

May 22, 2015 at 15:19

1 Answer 1

You can replace your application by the one being called with exec .

// Note: this waits for the exectued program to finish // before the call to `system()` returns. system("C:\xampp\xampp-control.exe"); // You can replace the current processes with the one you // are starting like this. execl("C:\xampp\xampp-control.exe", "xampp-control.exe"); // If this returns the applicaion failed to start. std::cerr  

C++ program termination

The exit function, declared in , terminates a C++ program. The value supplied as an argument to exit is returned to the operating system as the program's return code or exit code. By convention, a return code of zero means that the program completed successfully. You can use the constants EXIT_FAILURE and EXIT_SUCCESS , also defined in , to indicate success or failure of your program.

abort function

The abort function, also declared in the standard include file , terminates a C++ program. The difference between exit and abort is that exit allows the C++ runtime termination processing to take place (global object destructors get called). abort terminates the program immediately. The abort function bypasses the normal destruction process for initialized global static objects. It also bypasses any special processing that was specified using the atexit function.

Microsoft-specific: For Windows compatibility reasons, the Microsoft implementation of abort may allow DLL termination code to run in certain circumstances. For more information, see abort .

atexit function

Use the atexit function to specify actions that execute before the program terminates. No global static objects initialized before the call to atexit are destroyed before execution of the exit-processing function.

return statement in main

The return statement allows you to specify a return value from main . A return statement in main first acts like any other return statement. Any automatic variables are destroyed. Then, main invokes exit with the return value as a parameter. Consider the following example:

// return_statement.cpp #include struct S < int value; >; int main() < S s< 3 >; exit( 3 ); // or return 3; > 

The exit and return statements in the preceding example have similar behavior. Both terminate the program and return a value of 3 to the operating system. The difference is that exit doesn't destroy the automatic variable s , while the return statement does.

Normally, C++ requires that functions that have return types other than void return a value. The main function is an exception; it can end without a return statement. In that case, it returns an implementation-specific value to the invoking process. (By default, MSVC returns 0.)

Destruction of thread and static objects

When you call exit directly (or when it's called after a return statement from main ), thread objects associated with the current thread are destroyed. Next, static objects are destroyed in the reverse order of their initialization (after calls to functions specified to atexit , if any). The following example shows how such initialization and cleanup works.

Example

In the following example, the static objects sd1 and sd2 are created and initialized before entry to main . After this program terminates using the return statement, first sd2 is destroyed and then sd1 . The destructor for the ShowData class closes the files associated with these static objects.

// using_exit_or_return1.cpp #include class ShowData < public: // Constructor opens a file. ShowData( const char *szDev ) < errno_t err; err = fopen_s(&OutputDev, szDev, "w" ); >// Destructor closes the file. ~ShowData() < fclose( OutputDev ); >// Disp function shows a string on the output device. void Disp( char *szData ) < fputs( szData, OutputDev ); >private: FILE *OutputDev; >; // Define a static object of type ShowData. The output device // selected is "CON" -- the standard output device. ShowData sd1 = "CON"; // Define another static object of type ShowData. The output // is directed to a file called "HELLO.DAT" ShowData sd2 = "hello.dat"; int main()

Another way to write this code is to declare the ShowData objects with block scope, which implicitly destroys them when they go out of scope:

Komputer proqram teminati

A program must be terminated once its stated objectives have been met. Like any other language, C++ offers a variety of ways to terminate a program.

The following different methods will be discussed here to terminate a C++ program:

  1. abort() function
  2. terminate() function
  3. exit() function

Let’s start discussing each of these in detail.

abort()

abort function present inside a standard library header file. It terminates the program faster than other methods. The SIGABRT signal is raised by the abort function, which results in an abnormal termination of the current program. It is advised to utilize it only in the most extreme situations because it results in abnormal program termination.

The abort() function terminates the program without destroying the object and calling function passed to at_exit() or at_quick_exit() functions during the termination.

Syntax :

Parameter: This function doesn’t accept any parameter.

Return Value: This function doesn’t return any value.

Example 1: Below is the C++ program to abort the program after printing the welcome message.

C++

// C++ program to abort
// program using abort()
using namespace std;
// Driver code
cout << "Welcome To GeeksforGeeks !" << // This code will not execute cout << "A computer science portal " ;

Output:

Welcome To GeeksforGeeks ! Aborted

Explanation: In this program, the two print statements are used for two display messages and the abort function is called after one message. The abort () function is invoked when the first display message prints its output when the program gets executed. The program ends when the abort function is called. As a result, the sentence that follows the abort() method won’t be executed.

Example 2: Below is the C++ program for Abort() function in file handling.

C++

// C++ program for abort()
// function in file handling
// Driver code
// Create a file pointer
ptr = fopen ( "sample.txt" , "r" );
// Check file is NULL or not
if (ptr == NULL)
fputs ( "No such file found \n" ,
// Close file
fclose (ptr);

Output:

No such file found Aborted

Explanation: This program checks if a given file is present or not. If the file is not present, then it will print the “not found” message and abort the program.

exit ()

The exit() function is declared in the standard library header file. The exit function is used to exit or terminate the program. The Exit function exits a program while ignoring the rest of the code.
In other words, statements or codes after the exit function declaration are ignored by the system or will not be executed.
EXIT_SUCCESS and EXIT_FAILURE are constants that are defined in header files to indicate exit success or exit failure of programmer code.

Syntax:

Parameter: The exit function accepts a value as a Parameter. Every value except 0 indicates that the code was successfully terminated and every value except 0 indicates an error.

Return Value: The exit function doesn’t return a value.

Example 1: Below is the C++ program to implement exit():

Comments are closed, but trackbacks and pingbacks are open.