14

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

If Yes, how and why?

If No, why?

roottraveller
  • 6,328
  • 4
  • 50
  • 59
rajvelu
  • 151
  • 1
  • 3
  • Dupe http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c –  May 23 '10 at 10:05
  • see also - https://softwareengineering.stackexchange.com/questions/203104/why-cant-main-return-a-double-or-string-rather-than-int-or-void – roottraveller Sep 11 '17 at 11:18

6 Answers6

7

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

See here.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
Yin Zhu
  • 16,528
  • 12
  • 67
  • 111
  • Perhaps it's more of a conventional thing. It would be confusing if an application could return a char[], int[], etc... – Warty May 23 '10 at 06:43
  • 1
    @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? – Mahmoud Al-Qudsi May 23 '10 at 07:03
  • @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 May 23 '10 at 07:26
  • 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 May 23 '10 at 07:33
  • 1
    @YinZhu the link is broken. you should at least add some basic info here. – roottraveller Sep 11 '17 at 11:20
3

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
  • 4,801
  • 1
  • 28
  • 56
1

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
  • 668
  • 3
  • 8
  • 1
    $? 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 May 23 '10 at 07:22
  • I think we knew what $? was. Also, +1 for interesting diversion. – Daniel Harms May 23 '10 at 07:39
  • Sorry @Precision - I misread your question re: $?. I keep forgetting the answers can be edited after the comments have been made :) – pdbartlett May 24 '10 at 21:26
1

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
  • 32,014
  • 15
  • 104
  • 162
0

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
  • 437,173
  • 71
  • 570
  • 1,035
0

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
  • 8,026
  • 1
  • 18
  • 20