Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In many languages such as C, C++, and Java, the main method/function has a return type of void or int, but not double or String. What might be the reasons behind that?

I know a little bit that we can't do that because main is called by runtime library and it expects some syntax like int main() or int main(int,char**) so we have to stick to that.

So my question is: why does main have the type signature that it has, and not a different one?

share|improve this question
14  
What would a double return value mean? What would a string return value mean? –  delnan Jun 28 '13 at 20:48
1  
k i understand that it doesn't mean anything.But any other reasons and conventions? –  500_PLUS Jun 28 '13 at 20:55
1  
i think it doesn't mean anything,simply because universally it was choosen that 0 for normal exit and a non zero for a abnormal.An int was chosen as the simplest data type with broad cross language compatibility.@delnan –  500_PLUS Jun 28 '13 at 21:06
    
@sunny From what I've been able to gather from my experience with Unix-like OSs, 0 is used as a "normal exit" (0 errors) because it's unambiguous when compared to other integer values. Since most (not all) modern languages are designed to be be similar to (if not designed on the back of) C, and since C was used to write Unix, I'd say it was a historical decision by KnR. –  Jamie Taylor Jul 5 '13 at 8:03
3  
@sunny "broad cross language compatibility" was not an issue. C and UNIX were written in tandem. The reason many other languages return ints is because they were designed to work in UNIX or UNIX-like environments. –  user4051 Jul 5 '13 at 11:56

3 Answers 3

up vote 81 down vote accepted

The return value of main is to be passed to the operating system (any operating system) in a single, consistent way. The information that the operating system needs to know is "did the program terminate successfully, or was there an error?"

If this is a string, the response becomes difficult in different languages. The internals of a Pascal string (first byte is length) and a FORTRAN string (fixed, padded to some value) and a C string (null terminated) are all different. This would make returning a consistent value to the operating system challenging. Assuming that this was solved, what would you do to answer the question the OS had of the program? String comparisons are fraught with errors ("success" vs "Success"), and while the error may be more useful to a human, it is more difficult for the operating system or another program (shell) to deal with. There also were significant differences even in the strings themselves -- EBCDIC (with all of its code pages) vs. ASCII.

Floats and doubles provide no additional value over the integer for communicating back data to the OS (and shell). For the most part, neither of these parts of the computer deal with floating point numbers. Doubles are also not enumerable making comparisons difficult. Not being enumerable, they make reporting what the error was (assuming you have picked a particular value for success). Again, floating points are not consistent - a float on an 8 bit machine was different than the float on a 16 bit and a 32 bit machine (and those are just the 'normal' ones - even within IBM, floating point wasn't standardized between machines by the same manufacturer until the 1980's). And then you've got decimal vs. binary computers. Floating point values aren't consistent and don't provide meaningful data back.

That really leaves us with the byte and integer as options. The convention that was established was '0' was success, and anything else was an error. An integer gives more room than a byte for reporting the error. It can be enumerated (return of 1 means XYZ, return of 2 means ABC, return of 3, means DEF, etc..) or used as flags (0x0001 means this failed, 0x0002 means that failed, 0x0003 means both this and that failed). Limiting this to just a byte could easily run out of flags (only 8), so the decision was probably to use an integer.

share|improve this answer
2  
I think main is called by c/c++ runtime library before os call it which is also a piece of code loaded along with our code and called by the os@MichaelT –  500_PLUS Jun 28 '13 at 21:18
5  
main() is invoked different ways on different operating systems. In C, how is the main() method initially called? goes into this. –  MichaelT Jun 28 '13 at 21:27
19  
I think that the key point to understand is that main - unlike other functions in any program - is not part of a protocol defined by the programmer, but the protocol used to interface with the host (OS). You don't get to pick it because it was never yours to pick. At a more pragmatic level, UNIX expects an int to be returned by a process, and so the C-to-UNIX protocol does exactly that. An analogous argument can be made for argument-passing: if C had been invented for an OS/host that passed only numbers as arguments (e.g. no command-line), the arguments would be ints instead of strings. –  Euro Micelli Jun 29 '13 at 5:14
2  
IBM took the concept of code pages from EBCDIC to their PCs. They still haunt us today, going on 35 years after the introduction of the IBM 5150. 7-bit ASCII is codepage-less, but 8-bit character codes can be interpreted in many different ways even on a single computer, depending on settings -- let alone the codepages that code for multi-byte encodings. So it's actually even worse than what you allude to in the last sentence of the second paragraph. –  Michael Kjörling Jul 1 '13 at 11:17
    
@EuroMicelli that's a very nice information actually thanks for that :) –  500_PLUS Jul 9 '13 at 13:23

Well, it could.

For example, in the dialect of C used in the Plan 9 operating system main is normally declared as a void function, but the exit status is returned to the calling environment by passing a string pointer to the exits() function. The empty string denotes success, and any non-empty string denotes some kind of failure. This could have been implemented by having main return a char* result.

And it would certainly be possible to implement a system with a float or double exit status.

So why int? It's just a matter of convention -- and there's a tremendous value in having operating systems and programs that run under them obey a common convention.

The Unix convention is to use an integer status code, with 0 denoting success and non-zero denoting failure (because typically there's only one way to succeed, but multiple ways to fail). I don't know whether that convention originated with Unix; I suspect it came from earlier operating systems.

Floating-point would be a more difficult convention, because (a) floating-point support is not universal, (b) it's more difficult to define a mapping between floating-point values and error conditions, (c) different systems use different floating-point representations, and (d) just imagine the fun of tracking down a rounding error in your program's exit status. Integers, on the other hand, lend themselves very well to enumerating error codes.

Plan 9, as I mentioned, uses strings, but that imposes some complexity for memory management, character encoding, etc. It was, as far as I know, a new idea when Plan 9 implemented it, and it didn't replace the existing widespread convention.

(Incidentally, in C++ main can only return int, and in C void main is permitted only if the compiler specifically supports it. Many compilers don't complain very loudly if you write void main, but it's only a slight exaggeration to say that it's wrong.)

share|improve this answer
    
Thanks for your very useful answer :) –  500_PLUS Jul 1 '13 at 4:22

The value returned by the main method is an "exit code". It's used by the caller application (normally bash) to test if the program ended as expected. Returning an integer is the easiest way to do it on the OS level. Double makes no sense for error code and a String is hard to maintain in OS level (there is no GC).

share|improve this answer
2  
Why does a string need to be garbage collected while a integer does not? –  Brad Jun 28 '13 at 21:24
4  
@Brad, strings have variable length to them and would be in essence the same as passing back an array which could be one character or it could be thousands. Dynamic memory would be a pain while an int is a rather fixed size that isn't so hard to handle. –  JB King Jun 28 '13 at 21:45
    
@JBKing I like it. Thanks –  Brad Jun 28 '13 at 22:14
    
Short but effective answer. Thanks –  500_PLUS Jul 18 '13 at 1:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.