I am composing C code, in which ns am assessing some data. I have set the program to manage only 100 data inputs. As soon as it has much more than 100 inputs, it is providing a segmentation fault. I want to create a means so that when the number of inputs is over 100 the user will certainly be warned and also the routine will terminate. Ns know just how to perform it from the main function by merely return 0, however I to be multiple function calls far from main and it is difficult to do that also a return 0 in this role will store it looping.

You are watching: How to end a program in c

Is there any kind of immediate means to end the whole program without gift in main?


*

*

The standard function exit is what you room looking for:

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

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

The status discussion is went back to the host environment.

It would be far better though if you resolved the segfault error.


*

You need to incorporate the traditional lib and then girlfriend can contact exit wherever you want:

#include ...exit(status);where status is an integer representing the exit code.For what worry status: because that the convention 0 is success, other values shows an error status.


*

You can likewise fprintf(stderr, ....) and also then call abort(); this deserve to be beneficial if you desire to debug later on your bug.

But I think you should recode your regimen so the size constraints are only provided by available resources: therefore if you run your regime on a much bigger computer (whatever that means) it can process an ext than 100 inputs.


*

A slightly far better thing to do might be to capture the segment fault with a signal handler, and have the signal handler progressive a SIGSTOP signal.

That means your routine stops, but stays in memory. You can then connect to it v a debugger and also see the regime in all its glory frozen in location at the suggest the SEGFAULT happened.

That deserve to be helpful for finding troubles in lengthy running programs the you aren"t running inside a advance IDE.


Thanks for contributing an answer to ridge Overflow!

Please be sure to answer the question. Carry out details and share your research!

But avoid

Asking because that help, clarification, or responding to other answers.Making statements based upon opinion; ago them up with recommendations or an individual experience.

See more: When Did Martin Truex Jr Get Married, Martin Truex Jr

To find out more, watch our advice on writing great answers.


article Your prize Discard

By click “Post your Answer”, girlfriend agree to our regards to service, privacy policy and cookie plan


Not the price you're spring for? Browse other questions tagged c departure or questioning your very own question.


site design / logo design © 2021 stack Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.9.21.40262


your privacy

By click “Accept all cookies”, girlfriend agree ridge Exchange have the right to store cookie on your machine and disclose info in accordance through our Cookie Policy.