End program instruction.

Is there a instruction in C++ to exit the program if a condition is true, like:

if (st == 1) printf("The number is one") [INSTRUCTION GOES HERE];

That's my question, thanks in advance and apologize if I'm bothering someone.
You can use either return statement or function std::exit or even std::abort
Last edited on
Use: if (st == 1) { printf("The number is one"); exit(0); }; // or return 0;

You need process.h for exit().
@Amol Bhave

You need process.h for exit().


exit is a standard C/C++ function declared in stdlib.h (for C) or cstdlib (for C++) header.
Last edited on
@vlad from moscow

You can use either return statement or function std::exit or even std::abort


That's from #include <iostream.h> library, right?
exit and abort are standard C/C++ functions that are declared correcpondingly in stdlib.h and cstdlib headers. return is a statement of the C/C++.
In other words, return 0; can be an exit instruction? Sorry, I don't really understand, thank you.
1
2
3
4
5
6
7
8
9
// main is a function, so it has a return value like any other function.
int main()
{
    if (true)
        // When main returns, the program closes.
        return 0;
    else
        return 1;
}
Last edited on
OK. Now I understand it, but can I put return 0; as many times I want?
Do return 1; change the result of am algorithm?
exit(0);
OK. Now I understand it, but can I put return 0; as many times I want?
Do return 1; change the result of am algorithm?


No. return terminates a function. If the function is main(), then the program terminates also.

To terminate the program from some other function, you must call exit().


The number is the error code. Zero means your program is terminating willfully under "successful" conditions.

BTW, exit() does not do any resource cleanup. If you have any important RAII to handle, you need to do that first.

Good luck!
@manudo
OK. Now I understand it, but can I put return 0; as many times I want?
Do return 1; change the result of am algorithm?


You may use as many return statements as you want. The return statement does not change algorithms. It sets the point of exiting of a function and if the function is declared such a way that it shall return a value it also sets the return value.
Last edited on
Right, thank you guys.
return 0; alone is ambiguous. Depending on where you put it, it may or may not end your program. Take this for example.


1
2
3
4
5
6
7
8
9
10
11

int foo()
{
    return 0;
}

int main(int argc, char **argv)
{
    int myreturn= foo(); //even though foo returns 0, the program resumes
    return myreturn; //program exits when main returns
}


Alternatively, if you -do- want to return from foo(), you can use exit(0); the exit() function can be found in the stdlib.h
Last edited on
BTW, exit() does not do any resource cleanup. If you have any important RAII to handle, you need to do that first.


Partially correct. On Unix-like systems it *does* close the file/socket descriptors, including stdin, stdout and stderr. The system also releases all allocated memory for you, closes memory mapped files, etc. So there is no need to worry about any system-wide resource leak. However, it doesn't flush any buffers before that, so if you have some buffered, but not yet flushed to disk data, you can lose them.

If you don't want to do any resource cleanup, use _exit instead of exit, but that is usable only if you are using e.g. fork or vfork.
Last edited on
Yes, I should have made that clear. Using exit(0) is very much like just crashing your program and having the OS clean up. This can lead to loss of data and, in more advanced applications, some other unfriendly screw-ups.
Topic archived. No new replies allowed.