tags:

views:

243

answers:

6

Can the main function in a C program return a double data type?

If yes, how and why?

If no, why?

+3  A: 

Yes. According to C standard, main() should return a int value. But not must.

See here.

Yin Zhu
Perhaps it's more of a conventional thing. It would be confusing if an application could return a char[], int[], etc...
ItzWarty
@ItzWarty: You *can't* return a char[] or int[] or anything other than a primitive datatype. In whose memory would it be stored? Those are pointers, and they point *somewhere*. If the program has been destroyed, where would this array be stored?
Computer Guru
@Comp, it would be perfectly feasible for an operating system to allow you to copy a char[] returned from main to the parent process. This is no different in passing the int return code back to the parent (different storage requirements but exactly the same method).
paxdiablo
@Yin Zhu, when you changed your answer after more investigation, you forgot to change the 'No' to a 'Yes'. Please confirm that my fix was want you wanted.
paxdiablo
@ paxdiablo.thanks!
Yin Zhu
A: 

I don't know what it would mean if main returned double. Anyway here is what happens with gcc:

double main ()
{
    return 0.0;
}
$ cc double.c
double.c: In function 'main':
double.c:2: warning: return type of 'main' is not 'int'
$ ./a.out
$ echo $?
255
$
Snake Plissken
What's $? after this?
Precision
@Precision: noted
Snake Plissken
@Snake Plissken: Very interesting. I wonder why this is.
Precision
$? shows exit code of last command. As for why, @TridenT's answer above shows it's undefined. There's always some "plumbing" goes on before and after main is called, so I guess that provides a platform-specific "default" retcode when main's return value is incompatible with int.
pdbartlett
I think we knew what $? was. Also, +1 for interesting diversion.
Precision
Sorry @Precision - I misread your question re: $?. I keep forgetting the answers can be edited after the comments have been made :)
pdbartlett
@pdbartlett C'est la vie.
Precision
A: 

The function named main that acts as the entry point of a program in a hosted implementation of C must return an int. It's possible to write a function named 'main' that returns a double. To do so, you make it static, and then you buy yourself a bullet-proof vest and probably hire some armed guards; if anybody has to maintain such a monstrosity, you'll need them.

Jerry Coffin
+3  A: 

From C99

5.1.2.2.3 Program termination If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified

Reference here - 5.1.2.2.3 Program termination http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

TridenT
A: 

If you are in a hosted environment (i.e., usually on a PC) the return value type is fixed as already been said. You can return a different value type if running in a freestanding environment. A freestanding environment is usually the case when programming for microcontrollers, as there is no operating system around (you create one large binary that is run directly on the hardware). However, in this case the usual prototype for main would be void main(void).

But what's the point in returning a different type like double? Unless the caller (i.e. the operating system) can do something with the return value it's pointless, and as there is no way for the caller to know about the type of the return value it has to be fixed.

bluebrother
A: 

C says its perfectly fine. POSIX, on the other hand, wants a whole number between 0 and 255. Generally, main() should return a value of EXIT_SUCCESS or EXIT_FAILURE unless specifically setting a status that is neither but still between 0 - 255 in order to make the status meaningful.

For instance, returning 114 might tell the calling init script about some condition that could be corrected by the script.

If, under a POSIX OS you attempt to return 3.14 from main(), the calling process will almost always see 255.

I'm not sure about other operating systems, but in general - what C will let you get away with isn't always agreeable to what the OS itself will let you get away with :) That's not at all POSIX specific.

Tim Post