void and int

Pages: 12
woah, i got it
you explained that the return value is for giving a "hint" to a system that the program was terminated successfully - cmiiw
the return value is for giving a "hint" to a system that the program was terminated successfully


More than a "hint". It explicitly tells what happened, and that can be quite useful. For instance, when you run a particular program, say, from a different program (think installations), you might need to know what happened, and if it did not go exactly as planned (return 0), maybe your installation program can say oh ok, it returned with value blah, which means blah, so we'll do blah instead to get around it.

So yea, more than a hint, but you seem to be on the right track anyway.

cheers
jleach wrote:
I think we use int because the 32 bit integer is the "native type".

I think you mean a "word". Anyway, an int doesn't have to be 32 bits long. AFAIK, the standard only mandates a minimum range for each type.
It depends on the operating system how it works. While defined as an int, you really cannot rely upon more than 128 values (0..127) in your exit code.

Windows systems are pretty simple. Just don't return 259 as an exit code. Whatever else you return is OK.
http://www.google.com/search?btnI=1&q=msdn+GetExitCodeProcess

POSIX systems have a little more information attached to the exit code. IIRC, the first 256 values (0..255) are available to you to return an exit code. The remainder of the process exit code is used by the system to provide information about the status of the process.
http://linux.die.net/man/2/wait

Some platforms completely ignore the exit code altogether.

Keep in mind that C was designed along with Unix... so the exit code is a fairly *nix-centric thing. Other systems either already had something primitive or have been modified/created with a process exit code simply because it is common/useful. In systems like VMS, for example, playing with exit codes is really obnoxious... er, here's something I dug up http://www.unh.edu/cis/docs/vms-to-unix/Articles/3.02.html

Anyway...
Duoas wrote:
Windows systems are pretty simple. Just don't return 259 as an exit code.



The first thing that I did when I read that was compiled and ran:
int main(){return 259;}

Nothing happened. Now I'm depressed.
read the msdn link
Actually, void main is valid C. :-@


This seems to disagree...maybe we talking about different standards or something? http://www2.research.att.com/~bs/bs_faq2.html#void-main
Read the second link I gave. ;-)
// Is returning a non-zero made for debugging? Like if you coded a safety feature where "if this happens, return 12" then you would see it returned 12 and therefore know where the problem was sort of, for run-time errors maybe?
Last edited on
I don't really get what the discussion is.
People that scream that void main is valid, go read the ISO14882:1998 and 9899:1990.
I have (had to for school), and it's not in it, so it's not valid C or C++.

If I write a compiler that will see aquaman main() as legal C or C++ doesn't make it so, even if every compiler in the world does the same. The only language where void main() is legal that I know of is c# (ISO 23270). (but I only know C based languages, I'm sure there more).
It's not just for debugging, it's also to let a parent process know whether the program was able to successfully finish its job.
It's just a regular error code.
@Raggers
Read the second link I gave.

[edit] Here it is for you: http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html [/edit]
Last edited on
I know what is in it. The return type is implementation-defined, that is why void or any other return type CAN be used in A implementation of C++. But that doesn't mean others will to. Int MUST be implemented in ALL implementations of C++. Keep in mind the ISO is talking about the implementation of C++ itself, not a programmer implementing C++ to write it's program.
Meaning any_Type main () is allowed to be implemented by the compiler, but only int main() is true C++.
(True C and C++ source code is supposed to be implemented independent.)
Last edited on
Apparently you are missing something, then.

In C++, main() must return int. Nothing else.

In C, a conforming implementation may return anything. However, all conforming implementations must support main() returning int. Hence, I agree with your sentiment (as do the vast majority of knowledgeable C veterans) that only int main is proper, because it is portable to all implementations of the standard.

The article I linked you also takes care to show how void main is only legal as a loophole in the wording of ISO/IEC 9899:1999.
completely agree with you Duoas, and sorry I mist the fact about the 1999 and 1990.
Never really looked in to the new standard.

I still think allowing implementation-defined stuff to be in a standard goes agents having a standard.
If we go there we can all forget about implementation independent code and start writing code multiple times to support different implementations or go back to assembler code. Now where is the fun in that xD
LOL agreed!
In This discussion, i can learn to tell others the reason of the validation of void main() and int main().. Thanks to all !! Great Discussion !!!
yeah it's important, but it's ignored by the others
Topic archived. No new replies allowed.
Pages: 12