Using exit functions and return values

Hi Community !

I see all these return values and exit functions in some samples.

Why some People use return values but they don’t need it in the programm?
→ return is enough but some People use return 1 or return 2 or return-1

Why some People use exit(0) and another People use exit(NULL)
for termination.

Why some People prefer return(EXIT_SUCCESS) and return(EXIT_FAILURE) for successful termination or for not succsessful
termination if they don’t look to the return value later?

What’s the brainchild behind this??


printf(“MsgSend() error : %s \n”,strerror(errno));

  return 2;

Thank you !!!

if you return a value from main, then that value will then be passed to exit(), so

int main() { return 0; }
int main() { exit(0); }
are equivalent.

The main issue is that main is considered an integer function, so it’s bad form to not return anything.

As for what value you pass to exit, it’s up to you, but convention is that you return 0 for success, non zero for failure (hence EXIT_SUCCESS and EXIT_FAILURE)

Thank You cburgess

If it’s bad to not return anything what could be happend if I return nothing.
Is it possible my programm have bugs if I return nothing?

C and C++ standard specifies that main() be define as returning an int, hence you cannot return nothing ;-) If you do that, according to standard behavior is undefined. I’m guessing nothing wrong will happen aside maybe a random value being return to the shell.

The idea behind this is to return a value to the parent process.

Thank you mario