473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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.
Nov 14 '05 #1
2 2607
sugaray <ru****@sohu.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF
Nov 14 '05 #2
sugaray wrote:
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.

--
My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
Note: Rot13 and convert spelled-out numbers to numerical equivalents.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: George Marshall | last post by:
Hi all, I'm trying to get the exit code of a program I want to exec in a python script in a portable manner. I've read that both os.system and os.spawnl are not a portable solution. I tried...
2
by: junky_fellow | last post by:
The main() function of my C program is doing some system calls. If any of my system call fails, I want to terminate the program and propagate the exact cause of exit (the error number) to the...
16
by: Fao, Sean | last post by:
As far as I can tell, the standard has defined three portable return codes from function main() (0, EXIT_SUCCESS, EXIT_FAILURE). Personally, on all platforms I have worked with, EXIT_SUCCESS is,...
5
by: QQ | last post by:
I know there are many functions that I can exit the program such as return 0, exit(0), exit(1),_EXIT(0) .... What are the difference between them? Thanks a lot!
17
by: jwaixs | last post by:
Hello, I was wondering, what's the difference between exit and return in the main() function? For me they both look the same, or aren't they? And if they aren't, which should I use in which...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
4
by: rohitsagar | last post by:
I want to do fork from a program Code is very simple, below is the code, it just execute a executable called a.exe, I want to run a.exe 600 times. #include<stdio.h> #include<stdlib.h> void...
46
by: Bill Cunningham | last post by:
I have heard that return -1 is not portable. So what would be the answer to portability in this case? What about exit(0) and exit (-1) or exit (1)? Or would it be best to stick with C's macros,...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.