0

I was just wondering, if I write float main(), why do I need to write a return statement? I know about the C99 standard, which states

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.

Since float is compatible with int, return statement should not be necessary?

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • 3
    Please justify '`float` is compatible with `int`' — that premise is false, I believe. – Jonathan Leffler Oct 22 '16 at 15:46
  • 1
    what makes you think that float is compatible with int... compatible in C almost means "the same", which the two definitely are not – Johannes Schaub - litb Oct 22 '16 at 15:46
  • 1
    `float main(),` is simply wrong, and never would do anything useful. – πάντα ῥεῖ Oct 22 '16 at 15:47
  • float is compatible with int assignment-wise but not memory-wise. but why would you want to do that? do you have floating point return codes? – Jean-François Fabre Oct 22 '16 at 15:47
  • 1
    Also, why do you write "why do we need to write return statement", but then write "since ... return statement should not be necessary". what motivates the earlier assertion? compiler behavior, someone's statement, ..? – Johannes Schaub - litb Oct 22 '16 at 15:48
  • float is not compatible with int. – n. 'pronouns' m. Oct 22 '16 at 15:48
  • NB: ISO/IEC 9899:2011 has a section §6.2.7 **Compatible type and composite type** on what it means for types to be compatible: _¶1 Two types have compatible type if their types are the same. Additional rules for determining whether two types are compatible are described in 6.7.2 for type specifiers, in 6.7.3 for type qualifiers, and in 6.7.6 for declarators._ – Jonathan Leffler Oct 22 '16 at 15:49
  • ISO/IEC 9899:2011: 5.1.2.2.1 Program startup. The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters or with two params. So if the compiler allows you to write float main is not C – Tinwor Oct 22 '16 at 15:52
  • Why do you want to declare `main()` as `float`? What would that even mean? Anyway, the language standard says you can't do that. `main()` must simply always declared as `int`, period. (There might be an exception that lets you get away with `void`, but definitely not `float`.) – Steve Summit Oct 22 '16 at 16:02
  • @JohannesSchaub-litb when I tried float main() , my compiler told me I needed to write a return statement. Since int main() does not require a return statement to terminate it and I had read that all types compatible with int do not require a return statement (since float is compatible with int assignment-wise), I was curious why float requires a return. Thanks anyway. I've just started with my CSE degree and I was curious. – rainbowveins Oct 22 '16 at 17:45
  • @rainbowveins "...my compiler told me..." which compiler was that? Because one example is not proof, which was what litb was asking. I am not sure about the "is compatible with"-thing, I myself always confuse it with "is implicitly convertible". Therefore I am not sure if "return type of main function compatible with int," is applicable, like n.m. suggests. You should clarify in your question which parts you derive from the standard text and which from observed compiler behavior, which may or may not be standard conform. Maybe you are not sure yourself and have to sort it out first? – towi Oct 25 '16 at 07:52

0 Answers0