Is there a way to force the program to end in a function other than main in C/C++?

Asked 2 years ago, Updated 2 years ago, 29 views

For example,

void Queue::push(int _data) {
    if (isFull()) {
        cout << "Queue is Full. You can't push the data. Exiting program..";
        //Forced to end here!
    }
    else {
    ~~
    }

Like above, in the main function or other function, when you call the push function, isFull(), is there a way to force the program to shut down instead of running it? I know the assert, but I want to know another way

c c++

2022-09-22 11:44

1 Answers

In C/C++, the function that terminates the program is exit().

In general, if you exit normally without errors, the process returns zero, otherwise it returns an error code. To handle this, you can use it with exit(0) and exit(number of error codes).

To pass the return value of a process without using the exit function, define the main function as follows and return a number.

int main(int argc,char** argv) {
  // ... Code
  return 0; // return code 0 as the end value of the process.
  // Or it can be used with return EXIT_SUCCESS.
}

With exit, the exit() function terminates the process from any location.

int main(int argc,char** argv) {
  // ... Code
  exit(0);
  // or exit(EXIT_SUCCESS);
}

Note: https://msdn.microsoft.com/ko-kr/library/k9dcesdd.aspx


2022-09-22 11:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.