Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I immediately close a program in C?

Tags:

I am writing C code, in which I am analyzing some data. I have set the program to handle only 100 data inputs. When it has more than 100 inputs, it is giving a segmentation fault. I want to create a way so that when the number of inputs is above 100 the user will be warned and the program will terminate. I know how to do it from the main function by simply return 0, however I am multiple function calls away from main and it is difficult to do that even a return 0 in this function will keep it looping.

Is there any immediate way to terminate the entire program without being in main?

like image 947
Syntax_Error Avatar asked Nov 01 '11 22:11

Syntax_Error


People also ask

How do you close a program in C?

And the exit() function can also return a value when executed, like the return statement. So the C family has three ways to end the program: exit(), return, and final closing brace.

How do you stop a program immediately?

To quickly force quit on Windows, use the keyboard shortcut Alt + F4. Make sure the app or program window is open when you click Alt + F4. You can also force quit on Windows by using the Task Manager or Command Prompt.

What is abort in C?

In the C Programming Language, the abort function raises the SIGABRT signal, and causes abnormal program termination that returns an implementation defined code indicating unsuccessful termination. Functions registered with atexit aren't called.


1 Answers

The standard function exit is what you are looking for:

Terminates the process normally, performing the regular cleanup for terminating processes.

First, all functions registered by calls to atexit are executed in the reverse order of their registration. Then, all streams are closed and the temporary files deleted, and finally the control is returned to the host environment.

The status argument is returned to the host environment.

It would be better though if you fixed the segfault error.

like image 56
K-ballo Avatar answered Nov 10 '22 21:11

K-ballo