#include <stdio.h>
#include <math.h>
int main(void)
{
int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}
what does the "return 0" indicate in the above program?
thanks. 9 1417 no************@hotmail.com wrote: #include <stdio.h> #include <math.h>
int main(void) { int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number)); return 0; }
what does the "return 0" indicate in the above program? thanks.
It indicates a success, i.e. the program finished with no errors.
This value is intended to be checked by the host environment (e.g. a
shell script) to determine the outcome of the program run. Standard C
also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE
(defined as 1, with obvious meaning), and the two are the only values
standard knows about, i.e. the only portable values to return to the
environment. To use either, you have to #include <stdlib.h>.
Cheers
Vladimir
--
My e-mail address is real, and I read it.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 no************@hotmail.com wrote: #include <stdio.h> #include <math.h>
int main(void) { int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number)); return 0; }
what does the "return 0" indicate in the above program? thanks.
It indicates that the main() function explicitly returns a value of zero to it's
caller. What the caller does with this return value depends on the nature of the
caller.
- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFDz5SdagVFX4UWr64RArWNAJ92HjC3hkFkFb12SXsyge XJ7YNh6ACg5bKA
OBEbSUzKAmEeywrj2UbE51U=
=hyvF
-----END PGP SIGNATURE-----
Vladimir S. Oka wrote: no************@hotmail.com wrote: #include <stdio.h> #include <math.h>
int main(void) { int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number)); return 0; }
what does the "return 0" indicate in the above program? thanks. It indicates a success, i.e. the program finished with no errors.
Yes.
This value is intended to be checked by the host environment (e.g. a shell script) to determine the outcome of the program run. Standard C also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE (defined as 1, with obvious meaning),
No. EXIT_FAILURE is defined with some value, not necessarily 1. On at
least some systems returning 1 will indicate *success*, and on such a
system it is highly doubtful that EXIT_FAILURE would be defined as 1.
and the two are the only values standard knows about, i.e. the only portable values to return to the environment. To use either, you have to #include <stdlib.h>.
To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE,
with 0 meaning success and the other two are obvious. A return value of
1 is most definitely *not* portable.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Vladimir S. Oka wrote: no************@hotmail.com wrote:[...] what does the "return 0" indicate in the above program? thanks.
It indicates a success, i.e. the program finished with no errors.
This value is intended to be checked by the host environment (e.g. a shell script) to determine the outcome of the program run. Standard C also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE (defined as 1, with obvious meaning), and the two are the only values standard knows about, i.e. the only portable values to return to the environment. To use either, you have to #include <stdlib.h>.
Vladimir is partly correct, but not entirely.
EXIT_SUCCESS is not necessarily zero and EXIT_FAILURE
is not necessarily one. (If they were always zero and
one, the macros would be fairly useless ...).
On a host system that has no notion of the success
or failure of a program, it is even conceivable that
EXIT_FAILURE == EXIT_SUCCESS. (Note that I wrote
"conceivable," not "likely.")
Summary: zero means "success," EXIT_SUCCESS means
"success" (possibly a different flavor), and EXIT_FAILURE
means "failure" -- and these three (or two, or one) values
are the only termination codes blessed by the Standard.
--
Eric Sosman es*****@acm-dot-org.invalid
Eric Sosman wrote: Vladimir is partly correct, but not entirely. EXIT_SUCCESS is not necessarily zero and EXIT_FAILURE is not necessarily one. (If they were always zero and one, the macros would be fairly useless ...).
On a host system that has no notion of the success or failure of a program, it is even conceivable that EXIT_FAILURE == EXIT_SUCCESS. (Note that I wrote "conceivable," not "likely.")
Summary: zero means "success," EXIT_SUCCESS means "success" (possibly a different flavor), and EXIT_FAILURE means "failure" -- and these three (or two, or one) values are the only termination codes blessed by the Standard.
Thanks Eric, you're right. I stand corrected.
Cheers
Vladimir
--
My e-mail address is real, and I read it.
Flash Gordon <sp**@flash-gordon.me.uk> writes:
[...] To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE, with 0 meaning success and the other two are obvious. A return value of 1 is most definitely *not* portable.
And since 0 is required to indicate success, there's probably no good
reason for an implementation to define EXIT_SUCCESS as anything other
than 0 -- but it could, and you shouldn't assume that EXIT_SUCCESS==0.
(There's little reason to make such an assumption anyway.)
IMHO, the standard would have been clearer and more consistent if it
had *not* required 0 to indicate success, just defining EXIT_SUCCESS
and EXIT_FAILURE with implementation-specific values. The requirement
causes some problems for VMS^H^H^H OpenVMS, where odd values denote
success and even values denote errors; the C runtime system has to
make special allowances for "return 0;".
But I suppose "return 0;" or "exit(0);" was too firmly entrenched --
and it would have required nearly *every* program to have a
"#include <stdlib.h>".
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote On 01/19/06 16:14,: Flash Gordon <sp**@flash-gordon.me.uk> writes: [...]
To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE, with 0 meaning success and the other two are obvious. A return value of 1 is most definitely *not* portable.
And since 0 is required to indicate success, there's probably no good reason for an implementation to define EXIT_SUCCESS as anything other than 0 -- but it could, and you shouldn't assume that EXIT_SUCCESS==0. (There's little reason to make such an assumption anyway.)
The very O/S you proceed to cite offers a good reason
for EXIT_SUCCESS != 0 ...
IMHO, the standard would have been clearer and more consistent if it had *not* required 0 to indicate success, just defining EXIT_SUCCESS and EXIT_FAILURE with implementation-specific values. The requirement causes some problems for VMS^H^H^H OpenVMS, where odd values denote success and even values denote errors; the C runtime system has to make special allowances for "return 0;".
In the Very Old Days, a C program's exit status was
passed back verbatim as a VMS condition code, so exit(0)
meant "failure" ("warning," actually -- there were five
severity levels) and exit(1) meant "success." At some
point DEC decided to bow to prevailing custom, and put
in a hack that mapped 0->1 and 1->0 but left other values
untouched. That way, exit(0) and exit(1) had the meanings
on VMS that were not too different from what people were
accustomed to on Unix, while VMS-aware programs could still
exit with more informative VMS codes.
Then the Standard came along, and we had EXIT_SUCCESS
and EXIT_FAILURE. If I recall correctly (it's been a few
years), DEC didn't define EXIT_SUCCESS as 0 and let the
library change it to 1, but defined it as a large odd
number, turning "generic success" into "success of C program."
Similarly, EXIT_FAILURE wasn't defined as 1 to be mapped to
0, but as a large even value meaning "failure of C program."
Sounds like a silly distinction when viewed in isolation,
but remember: VMS condition codes were structured, with
various fields conveying different pieces of information.
By picking apart the fields you could determine not only
success/failure, but a severity level (five were defined),
a specific condition (a program could succeed in many ways,
or fail in many ways), and the system component where the
condition arose. A bare zero or one omitted most of this
information, whereas EXIT_xxxx supplied it.
-- Er*********@sun.com
Eric Sosman <er*********@sun.com> writes: Keith Thompson wrote On 01/19/06 16:14,: Flash Gordon <sp**@flash-gordon.me.uk> writes: [...]
To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE, with 0 meaning success and the other two are obvious. A return value of 1 is most definitely *not* portable.
And since 0 is required to indicate success, there's probably no good reason for an implementation to define EXIT_SUCCESS as anything other than 0 -- but it could, and you shouldn't assume that EXIT_SUCCESS==0. (There's little reason to make such an assumption anyway.)
The very O/S you proceed to cite offers a good reason for EXIT_SUCCESS != 0 ...
Given that exit(0) is required to denote success, defining
EXIT_SUCCESS as 0 would be perfectly sensible even under VMS.
Defining it as some arbitary odd value could have some advantages,
though. For one thing, it might be somehow useful for exit(0) and
exit(EXIT_SUCCESS) to behave in some subtly different manner.
In fact, I just remembered I have an account on a VAX running OpenVMS
6.2. A quick test shows that EXIT_SUCCESS is defined as 0, and
EXIT_FAILURE is defined as 268435458 (0x10000002). IMHO, the standard would have been clearer and more consistent if it had *not* required 0 to indicate success, just defining EXIT_SUCCESS and EXIT_FAILURE with implementation-specific values. The requirement causes some problems for VMS^H^H^H OpenVMS, where odd values denote success and even values denote errors; the C runtime system has to make special allowances for "return 0;".
In the Very Old Days, a C program's exit status was passed back verbatim as a VMS condition code, so exit(0) meant "failure" ("warning," actually -- there were five severity levels) and exit(1) meant "success." At some point DEC decided to bow to prevailing custom, and put in a hack that mapped 0->1 and 1->0 but left other values untouched. That way, exit(0) and exit(1) had the meanings on VMS that were not too different from what people were accustomed to on Unix, while VMS-aware programs could still exit with more informative VMS codes.
Actually, if I recall correctly, it maps exit(0) to an odd value, but
it doesn't map exit(1). Back when I worked on VMS systems, I saw
programs that used exit(1) with the intent of indicating failure; they
didn't.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. no************@hotmail.com writes:
each function has a return value, so does the 'main'
function.
you'd better have each function with a return value
so that the caller could learn the result of the function
called.
in common case, the return value '0' means the function
is executed suceessfully. of course you may define other
error codes for it if anything abnormal occurs. #include <stdio.h> #include <math.h>
int main(void) { int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number)); return 0; }
what does the "return 0" indicate in the above program? thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: King Tut |
last post by:
I would like to display a progress bar (or some progress indication) when
the user hits a submit button until the request is completed and is
loaded.... Something that works like Flash, when you...
|
by: adri4n |
last post by:
as wat ive mentioned in the title.. im would like to know whether the a
particular record/table is being locked in my program. some of the
methods which i would like to develop are as below:
...
|
by: Dhruba Bandopadhyay |
last post by:
I have a problem.
I have a C++ service and added a new API to it. I re-registered the service
and re-added the reference in my ASP.NET Visual Web Developer. However when
I call the new API I...
|
by: darrel |
last post by:
We're getting this error on a page that uses a particular User Control we
created:
"An Error has Occured retrieving the news item.
Attempted to read or write protected memory. This is often an...
|
by: Sugandh Jain |
last post by:
Hi,
I am getting the error message
Attempted to read or write protected memory. This is often an indication
that other memory is corrupt.
It was not coming until yet, for around 2 months. Now,...
|
by: Mike |
last post by:
How can i handle "attempted to read or write protected memory. this
is often an indication that other memory is corrupt"
I use VS 2005 framework 2.0
On the server explorer window I try to open...
|
by: yeah |
last post by:
hi
My compilation result includes some indication starts with
"note:xxxxxxxxxxx"
what is the use of this indication?
and How to disable this??
thanks in advance
|
by: harikumar001 |
last post by:
Hi all,
I was working with VB 2005 to create an application that will send and receive SMS using a GSM Modem(Siemens MC35i). I am not pretty sure about the AT+CNMI parameters I am supposed to use so...
|
by: dzenanz |
last post by:
I have JSP web-form which uploads a file. In java code, I have a statement like this:
for all lines in txt file uploaded
call DB stored procedure
As this web interface should be used over a...
|
by: McLovin |
last post by:
Hi,
I have this error:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
const char* char_9;
string str_char_9;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |