Reputation: 171
Can the main()
function in a C program return a double
data type?
If Yes, how and why?
If No, why?
Upvotes: 16
Views: 11012
Reputation:
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.
Upvotes: 1
Reputation: 17119
Yes. According to C standard, main() should return a int
value. But not must.
Upvotes: 8
Reputation: 8886
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.
Upvotes: 0
Reputation: 678
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 $
Upvotes: 1
Reputation: 4909
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
Upvotes: 3
Reputation: 490108
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.
Upvotes: 0