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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With