I have a function()
which calls anotherFunction()
.
Inside anotherFunction()
, there is an if
statement which, when satisfied returns back to main()
and not to function()
. How do you do this?
How to return to main and not the function calling it?
163 views Asked by bee. At
4
There are 4 answers
2
On
You can't do like that in "standard" C. You can achieve it with setjmp and longjmp but it's strongly discouraged.
Why don't just return a value from anotherFuntion()
and return based on that value? Something like this
int anotherFunction()
{
// ...
if (some_condition)
return 1; // return to main
else
return 0; // continue executing function()
}
void function()
{
// ...
int r = anotherFuntion();
if (r)
return;
// ...
}
You can return _Bool
or return through a pointer if the function has already been used to return something else
You can bypass the normal return sequence in C with the setjmp and longjmp functions.
They have an example at Wikipedia: