-3

Do we have function overloading in C? How the main() function works ? If we use int main() or void main() or char main(), it works pretty much well. How this is managed in C?

Krishna
  • 49
  • 6
  • 1
    In languages that support it, the return type doesn't generally take part in the overloading process (except for some notable exceptions like Perl and Haskell). – Robert Harvey Aug 06 '20 at 17:29
  • 2
    Putting aside the fact that `void main()` and `char main()` are not valid signatures for `main`, this isn't overloading. Overloading allows different functions with the same name to exist **together** in one declarative region. What you describe always involves a **single** definition. – StoryTeller - Unslander Monica Aug 06 '20 at 17:31
  • Also here: https://stackoverflow.com/q/2351792/12139179 – RobertS supports Monica Cellio Aug 06 '20 at 17:33
  • `main()` can have different signatures in C because the C language standard specifies it for `main()`. Note that as @StoryTeller-UnslanderMonica mentioned, several of your signatures for `main()` aren't specified by the standard. The fact that your compiler allows them is either oversight or an extension to standard behavior. – Michael Burr Aug 06 '20 at 17:40
  • 1
    And... unless you are programming for a freestanding environment, everything except `int main(void)` and `int main (int argc, char *argv[])` are wrong. [C11 Standard - 5.1.2.1 Freestanding environment](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.1) and [C11 Standard - §5.1.2.2.1 Program startup(p1)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1) – David C. Rankin Aug 06 '20 at 17:40

1 Answers1

1

Do we have function overloading in C?

No.

Function identifiers always have either external linkage or internal linkage. Within an entire program, each identifier with external linkage that is ever accessed refers to exactly one function or object. (An identifier that is never referenced might not refer to any function or object that actually exists.) Within any translation unit, an identifier that elsewhere has external linkage can be declared to have internal linkage instead, in which case it instead identifies a (one) function or object appearing elsewhere in the same translation unit.

Under all circumstances, within any given translation unit, an in-scope function identifier that refers to an existing function refers to exactly one existing function.

How the main() function works ? If we use int main() or void main() or char main(), it works pretty much well.

For the most part, main() works just like any other function, but there are extra rules for it. Specifically, a strictly conforming C program has a choice of one of two signatures for main():

int main(void)

and

int main(int argc, char *argv[])

(or equivalent formulations). Each program must choose one. It is the C implementation's job to perform any adaptation needed to the choice between those signatures.

If your program declares main() with a return type of anything other than int, then it is non-(strictly-)conforming. Any particular implementation might nevertheless accept other forms, and those might seem to "work pretty much well" with such an implementation, but the language standard provides no basis for expecting any particular behavior at all from such a program.

Note also that alternative signatures for main() do not constitute function overloading. There is under any circumstance only one external function named main in any program.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157