Can main() return any int value?

A

Aggro

Hi,

I would need to return at least two different values from my program
depending on the results of it for shell script to identify which the
result was, but I would also like the program to follow the C++ standards.

I have seen posts that claim that C++ program can return only 0,
EXIT_SUCCESS or EXIT_FAILURE. Is this is true, or can I just return 0, 1
or even 10234 from my program? Or is only 0 allowed?

What part of the C++ standard says so?

TIA
 
E

Erik Wikström

Hi,

I would need to return at least two different values from my program
depending on the results of it for shell script to identify which the
result was, but I would also like the program to follow the C++ standards.

I have seen posts that claim that C++ program can return only 0,
EXIT_SUCCESS or EXIT_FAILURE. Is this is true, or can I just return 0, 1
or even 10234 from my program? Or is only 0 allowed?

What part of the C++ standard says so?

Using return in main will call exit() with the value returned, regarding
exit() the standard has the following to say in 18.3/4:

"Finally, control is returned to the host environment. If status is zero
or EXIT_SUCCESS, an implementation-defined form of the status successful
termination is returned. If status is EXIT_FAILURE, an implementation-
defined form of the status unsuccessful termination is returned.
Otherwise the status returned is implementation-defined."
 
J

Joe Smith

Aggro said:
Hi,

I would need to return at least two different values from my program
depending on the results of it for shell script to identify which the
result was, but I would also like the program to follow the C++ standards.

I have seen posts that claim that C++ program can return only 0,
EXIT_SUCCESS or EXIT_FAILURE. Is this is true, or can I just return 0, 1
or even 10234 from my program? Or is only 0 allowed?

What part of the C++ standard says so?

TIA

The meaing of the return value is implementation defined. The values 0 and
EXIT_SUCCESS will be intepreted by the implementation as sucessful
completion of the program, while EXIT_FAILURE is interpreted an unsucessful
completion of the program. The implementation may ignore the return value,
but must not treat a success case as a failure or vice versa.

However, if you return any other value the implementation may interpet it in
any way desired by the implementation creator. In practice this is a
non-issue. Most operating systems treat 0 as success and non-zero as
failure, and apply no further meaning to the code, but makes the return
value available to the parent process, such that it could interpret the
value further. A few systems have conventions on return values that other
programs tend to expect, but that is well outside the area of Standard C++.
 
S

sean_in_raleigh

I have seen posts that claim that C++ program can return only 0,
EXIT_SUCCESS or EXIT_FAILURE. Is this is true, or can I just return 0, 1
or even 10234 from my program? Or is only 0 allowed?

You might be interested in what the POSIX standards say about
exit() (which gets called with your main() return value):

The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, or any
other value, though
only the least significant 8 bits (that is, status & 0377) shall be
available to a
waiting parent process.

Source: http://www.opengroup.org/onlinepubs/9699919799/functions/exit.html#tag_16_112

Of course that's not binding on C++ as a language, but they're telling
you what the implementation will do if it's POSIX-compliant.

So yes, you can return 0, 1, or 10234 from your program, but it will
be
as if you returned 0 & 0xff, 1 & 0xff and 10234 & 0xff (i.e., 250),
respectively.

Sean
 
A

Aggro

Erik said:
Using return in main will call exit() with the value returned, regarding
exit() the standard has the following to say in 18.3/4:

Thank you, that was what I was looking for.
 
P

Pascal J. Bourguignon

Aggro said:
I would need to return at least two different values from my program
depending on the results of it for shell script to identify which the
result was, but I would also like the program to follow the C++
standards.

I have seen posts that claim that C++ program can return only 0,
EXIT_SUCCESS or EXIT_FAILURE. Is this is true, or can I just return 0,
1 or even 10234 from my program? Or is only 0 allowed?

What part of the C++ standard says so?

Any value can be returned by main:

#include <ciso646>
#include <string>
#include <iostream>

int main(int argc,char** argv){
if((argc==2) and (std::string("recurse")==argv[1])){
return(424242);
}else{
char* args[]={"main","recurse",0};
std::cout<<"main(2,{\"main\",\"recurse\",0})=="<<main(2,args)<<std::endl;
return(0);
}
}

/*
-*- mode: compilation; default-directory: "~/src/tests-c++/" -*-
Compilation started at Fri Jan 9 09:39:46

SRC="/home/pjb/src/tests-c++/main.c++" ; EXE="main" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} && ./${EXE} && echo status = $?
main(2,{"main","recurse",0})==424242
status = 0

Compilation finished at Fri Jan 9 09:39:46
*/


Now, as mentionned in the other answer, the C++ standard doesn't
ensure portable behavior for a _program_ returning anything else than
success or failure.


Now for the implementation dependant stuff, if we can assume you're
running on a unix system, then man 2 wait gives some information
about the status returned by unix programs. Basically, in general
only the least significant 8 bit of the value returned by main are
used for the status, but values greater than 128 are used to indicate
whether the program was killed on a signal (128+signum). You can
return them but callers could believe you were killed, not exited, if
they don't check the second byte, which contains various flags to
indicate how the program exited. (Have a look at
/usr/include/bits/waitstatus.h)
 
J

James Kanze

The meaing of the return value is implementation defined. The
values 0 and EXIT_SUCCESS will be intepreted by the
implementation as sucessful completion of the program, while
EXIT_FAILURE is interpreted an unsucessful completion of the
program. The implementation may ignore the return value, but
must not treat a success case as a failure or vice versa.

Except that what treating a program as a success is also
implementation defined.
However, if you return any other value the implementation may
interpet it in any way desired by the implementation creator.
In practice this is a non-issue. Most operating systems treat
0 as success and non-zero as failure,

I'm not sure about most. I don't know of any OS which even
looks at the return code itself. Both Windows and Unix simply
make it available (possibly modified, e.g. Unix and's it with
0xFF) to the program which invoked your program. (And at least
one OS treated even numbers as a failure, and odd as success;
the C implementations there had to map 0 to something odd.)
and apply no further meaning to the code, but makes the return
value available to the parent process, such that it could
interpret the value further. A few systems have conventions on
return values that other programs tend to expect, but that is
well outside the area of Standard C++.

Yes. Pretty much everything which happens outside your program
is not subject to the C++ standard. As far as the C++ standard
is concerned, all that is required is that the program return an
int; any value between INT_MIN and INT_MAX is valid. Beyond
that, it's almost entirely up to the implementation; all the
standard says is that 0 and EXIT_SUCCESS should map to something
the system considers success (for some definition of success),
and EXIT_FAILURE should map to something the system considers
failure.
 
J

James Kanze

You might be interested in what the POSIX standards say about
exit() (which gets called with your main() return value):
The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE,
or any other value, though only the least significant 8
bits (that is, status & 0377) shall be available to a
waiting parent process.

Of course that's not binding on C++ as a language, but they're
telling you what the implementation will do if it's
POSIX-compliant.

Which most aren't. In particular, it's not what Windows does.
So yes, you can return 0, 1, or 10234 from your program, but
it will be as if you returned 0 & 0xff, 1 & 0xff and 10234 &
0xff (i.e., 250), respectively.

Only under Unix.
 
R

Rolf Magnus

James said:
Except that what treating a program as a success is also
implementation defined.


I'm not sure about most. I don't know of any OS which even
looks at the return code itself.

It depends on what you define as "OS". If you look only at the kernel, then
probaly most don't look at the return code. If OTOH, you look at the wohle
system, things could be different, probably depending on the type of program
you're writing (GUI program, command line tool, system service, ...).
 
J

James Kanze

It depends on what you define as "OS". If you look only at the
kernel, then probaly most don't look at the return code. If
OTOH, you look at the wohle system, things could be different,
probably depending on the type of program you're writing (GUI
program, command line tool, system service, ...).

Yes. What actually comprises the "OS" is a very open question.
In this case, however, you can very well write a program under
Unix or Windows which does something like:

if ( system( "yourProgram" ) == 0 ) {
std::cerr << "yourProgram failed" << std::endl ;
}

It doesn't make sense to do so, but my point is simply that
neither Windows nor Unix require much of anything of the return
code, per se; the "convention" of what is failing and what isn't
is really just that, a convention, and the systems count on the
invoking program to respect it. Most programs delivered with
the system do, when they invoke another program. In the sense
that they're part of the system, the system does judge the
success or failure of your program. But since your program can
also be invoked otherwise, and not all "system" programs look at
the return code, either, there's also a very real sense that the
system doesn't even look at the code. (When I configure a panel
button under KDE to invoke a program, the return code will be
ignored, for example, and I'm pretty sure that the same holds
for configuring an icon on the desktop under Windows.)

Of course, this is not to say that you, as a programmer,
shouldn't care; you want your program to work in all reasonable
contexts. Including cases where the invoking program does look
at the return code.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top