Why noy 'return' not portable but exit() does ?

S

sugaray

i was just told in the 'simple base-conveter program' thread, that
using 'return' is not portable in such case:

if(argc!=2) {
fprintf("error message\n");
return 1;
}

it should be replace by 'exit(EXIT_FAILURE)',
but i don't get why's that ? isn't 'return' keyword of C ? why is not
portable, but exit() does ? can someone tell me why, or better, give
some examples showing the code is not portable by using 'return' ?
thanx.
 
J

Joona I Palaste

sugaray said:
i was just told in the 'simple base-conveter program' thread, that
using 'return' is not portable in such case:
if(argc!=2) {
fprintf("error message\n");
return 1;
}
it should be replace by 'exit(EXIT_FAILURE)',
but i don't get why's that ? isn't 'return' keyword of C ? why is not
portable, but exit() does ? can someone tell me why, or better, give
some examples showing the code is not portable by using 'return' ?
thanx.

You have been told wrong. It's the 1 that is not portable, not the
return. "return" is very much a keyword in C, and can be thought of
even more part of the language than exit(). Using "return
EXIT_FAILURE;" would be completely OK here. But using
"exit(1);" would not, as it would be non-portable.
 
A

August Derleth

sugaray said:
i was just told in the 'simple base-conveter program' thread, that
using 'return' is not portable in such case:

if(argc!=2) {
fprintf("error message\n");
return 1;
}

it should be replace by 'exit(EXIT_FAILURE)',
but i don't get why's that ? isn't 'return' keyword of C ? why is not
portable, but exit() does ? can someone tell me why, or better, give
some examples showing the code is not portable by using 'return' ?
thanx.

return is portable. return 1; is portable in subroutines that are not
main(). But doing a return 1; from main() means different things on
different systems: On Unix, for example, it means failure, whereas on
VMS it means success. I'm sure you'd agree, this could lead to nasty,
hard-to-find bugs when moving the sources from a Unix system to VMS, or
something like that.

EXIT_SUCCESS and EXIT_FAILURE, however, always mean what they say. They
are generally preferred in all new programs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top