473,387 Members | 3,810 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,387 software developers and data experts.

Return value of main

Good afternoon all,

I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else? In addition, if there has been some
failure, do people tend to return another value (such as -1?)

Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

Cheers,
Nick
Mar 10 '08 #1
11 2919
polas <ni**@helpforce.comwrote:
I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else?
The Standard requires three values to return a definite status to the
environment: 0 and EXIT_SUCCESS signal success, EXIT_FAILURE signals
failure. EXIT_SUCCESS and EXIT_FAILURE are macros #defined in
<stdlib.h>; what their actual values are is not defined by the Standard,
and may vary from implementation to implementation. Of course it is
highly common for EXIT_SUCCESS to be 0, and EXIT_FAILURE is often 1, but
neither of those is guaranteed.
If you return any other value from main() (or through exit()), this does
not make the program exhibit undefined behaviour; the only result is
that the meaning of the status returned to the environment is undefined
by the Standard. So calling exit(2) does not make your program print
weird things or write over memory it doesn't own; but it does mean that
the C Standard itself does not guarantee that other programs will see
this as your program having succeeded, failed, or otherwise.
Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
That depends entirely on the OS.
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?
It does; hence the existence of EXIT_SUCCESS and EXIT_FAILURE, which can
be adapted to the OS' needs.

Richard
Mar 10 '08 #2
polas wrote:
Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?
If you include <stdlib.h>, the symbols EXIT_SUCCESS and
EXIT_FAILURE will be defined appropriately.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Mar 10 '08 #3
On 10 Mar, 12:40, Morris Dovey <mrdo...@iedu.comwrote:
polas wrote:
Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

If you include <stdlib.h>, the symbols EXIT_SUCCESS and
EXIT_FAILURE will be defined appropriately.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USAhttp://www.iedu.com/DeSoto
Thanks for the replies - that clears it up completely for me.

Nick
Mar 10 '08 #4
polas wrote:
Good afternoon all,

I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else? In addition, if there has been some
failure, do people tend to return another value (such as -1?)
The only standard values for these are 0, EXIT_SUCCESS and EXIT_FAILURE.
Zero is the same as EXIT_SUCCESS. For using any other values (like
1, -1 etc.) you'll have to seek the guarantees of standards beyond ISO
C.
Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?
No such standard behaviour is specified by ISO C. Each system might have
it's own standardised or semi-standard behaviour. The portability of C
code in your example will not be affected because a return of zero
always means successful termination and the C library is obliged to
translate this to whatever code that the system uses for successful
termination. Thus if for the underlying system 10 indicates successful
termination and you return zero, the C library will actually return 10.
It will do the same for a return of EXIT_SUCCESS too. A recompile for
each platform will ensure correct behaviour.

Mar 10 '08 #5
On 10 Mar, 13:52, santosh <santosh....@gmail.comwrote:
polas wrote:
Good afternoon all,
I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else? In addition, if there has been some
failure, do people tend to return another value (such as -1?)

The only standard values for these are 0, EXIT_SUCCESS and EXIT_FAILURE.
Zero is the same as EXIT_SUCCESS. For using any other values (like
1, -1 etc.) you'll have to seek the guarantees of standards beyond ISO
C.
Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

No such standard behaviour is specified by ISO C. Each system might have
it's own standardised or semi-standard behaviour. The portability of C
code in your example will not be affected because a return of zero
always means successful termination and the C library is obliged to
translate this to whatever code that the system uses for successful
termination. Thus if for the underlying system 10 indicates successful
termination and you return zero, the C library will actually return 10.
It will do the same for a return of EXIT_SUCCESS too. A recompile for
each platform will ensure correct behaviour.
Right - just to clear up a minor point, is 0 defined by the standard
to be success (as well as EXIT_SUCCESS)?

Nick
Mar 10 '08 #6
polas <ni**@helpforce.comwrote:
Right - just to clear up a minor point, is 0 defined by the standard
to be success (as well as EXIT_SUCCESS)?
Yes. (As an aside, it's not _guaranteed_ that 0 and EXIT_SUCCESS are the
same kind of success, but in practice, I don't think there are many
ssytems which have more than one kind anyway.)

Richard
Mar 10 '08 #7
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
polas <ni**@helpforce.comwrote:
Right - just to clear up a minor point, is 0 defined by the standard
to be success (as well as EXIT_SUCCESS)?

Yes. (As an aside, it's not _guaranteed_ that 0 and EXIT_SUCCESS are the
same kind of success, but in practice, I don't think there are many
ssytems which have more than one kind anyway.)
IIRC, VMS has several kinds of success: every even result is a failure and
every odd one a success; the runtime does something particular in the case
of 0 so that it has its standard behaviour instead of the VMS one).

Yours,

--
Jean-Marc
Mar 10 '08 #8
Richard Bos wrote:
>
polas <ni**@helpforce.comwrote:
[...]
The Standard requires three values to return a definite status to the
environment: 0 and EXIT_SUCCESS signal success, EXIT_FAILURE signals
failure. EXIT_SUCCESS and EXIT_FAILURE are macros #defined in
<stdlib.h>; what their actual values are is not defined by the Standard,
and may vary from implementation to implementation. Of course it is
highly common for EXIT_SUCCESS to be 0, and EXIT_FAILURE is often 1, but
neither of those is guaranteed.
Well, it is guaranteed that EXIT_FAILURE cannot be defined as zero.
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?

It does; hence the existence of EXIT_SUCCESS and EXIT_FAILURE, which can
be adapted to the OS' needs.
Note that exit(0) will still signal "success", even in this case.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Mar 10 '08 #9
polas wrote:
Good afternoon all,

I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else? In addition, if there has been some
failure, do people tend to return another value (such as -1?)
Without including <stdlib.h>, there is only one portable value, 0, which
indicates successful completion.

If you include <stdlib.h>, you have available the values EXIT_SUCCESS
and EXIT_FAILURE.

Is there any standard for operating systems to handle the C return
value in a particular way (I know some ignore it and some do not) ?
For instance, if OS A regards return value 0 as successes and OS B
regards return value 0 as failure then surely this would affect the
portability of the C code?
There were C implementations that when the C program returned 0 would
indicate to the operating system that the program had failed. Such
implementations, as far as I know, are no longer used and obsolete. If
you want to indicate successful completion, return 0 or EXIT_SUCCESS.
If you want to portably indicate failure, return EXIT_FAILURE. Your
implementation and operating system might define meanings for other
values, but they are not portable or defined by the C language.
Mar 10 '08 #10
Kenneth Brody wrote:
Well, it is guaranteed that EXIT_FAILURE cannot be defined as zero.
That's not guaranteed.

Some systems don't make use of the return value of main,
and on systems that don't,
there is no reason to have any meaning at all
encoded into the values that main can return.

--
pete
Mar 10 '08 #11
user923005 wrote:
On Mar 10, 6:52*am, santosh <santosh....@gmail.comwrote:
>polas wrote:
Good afternoon all,
I appreciate that the standard requires that main should return an
integer. What I was wondering though is there any accepted
standardisation on exactly what value to return - do people tend to
return 0 or 1 or anything else? In addition, if there has been some
failure, do people tend to return another value (such as -1?)

The only standard values for these are 0, EXIT_SUCCESS and
EXIT_FAILURE. Zero is the same as EXIT_SUCCESS. For using any other
values (like 1, -1 etc.) you'll have to seek the guarantees of
standards beyond ISO C.
<snip>
So we can conclude that return 0 or return EXIT_SUCCESS will return
successful status and that return EXIT_FAILURE will return
unsuccessful status and that any other value must be implementation
defined.
That's what I believe I said too.

Mar 12 '08 #12

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

Similar topics

1
by: Piotr | last post by:
I have popup window (window.open) where I put any value in input field. After submit I wan to to return to the main window and get value from popup window. How to close popup window and return to...
32
by: Mike Machuidel | last post by:
Hi, I'm a game developer programming mostly in C and ASM for about 7 years. Today at work a colleague (a C++ programmer) yelled at me I'm a bad C programmer because I use "return(0);" instead...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
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...
25
by: Christian Christmann | last post by:
Hi, the ANSI-C 99 standard specifies that the main function has "int" as return type. However, there are still lots of people declaring "void" as main return type. Did previous ANSI-C...
37
by: Army1987 | last post by:
Is that in the object line a conforming program? If so, why? If not, why? I'd expect it to be much like int main(void) { for (;;); } But if I compile it with lcc-win32 and run it in its...
17
by: Sara | last post by:
Hi, I'm having a problem with my program and I think it stems from me not understand how to call a function and return a int value to main. What I have to do is create a program that runs...
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
27
by: junky_fellow | last post by:
Guys, Can I return 0, from main() ? Is this equivalent to exit(EXIT_SUCCESS) ? thanks for any help...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.