0

Is main a user-defined function or built -in function? Or is it both?

Like if I say declaration of main is predefined and definition is user defined, can I say it is both built-in and user-defined?

There is a discussion here: Is main() a User-Defined Function?

But I could not understand what they concluded with, or rather to be exact I was not satisfied with the answers. I differ from the above discussion on that link that main cannot be called. It can be called, but SHOULDN'T be called (correct me if this notion is wrong!).

Community
  • 1
  • 1
Tom
  • 125
  • 1
  • 11

4 Answers4

4

The C11 standard lists two kinds of environments: freestanding environment, meaning an embedded system or operative system, and hosted enviroment, meaning a program running on top of an OS.

5.1.2.1 Freestanding environment

In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.

In other words, in freestanding environments, the function called at startup could be called anything, and have any form. Most common is void main (void).

From C11, the chapter regarding hosted environment:

5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no prototype for this function.

The "implementation" means the compiler, so the compiler declares no such function. It is up to the user (programmer) to do so. This can be done in the form int main (void) or int main(int argc, char *argv[]) or in any implementation-defined way specified by the compiler. In any case, the function is defined by the user.

C++ is a bit stricter and enforces any of the two forms, and allows no implementation-defined version of main. From C++03 3.6.1:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }


Regarding whether main can be called or not: I don't believe there is anything in the C standard preventing this, even though calling main makes no sense whatsoever. Since it has no prototype, the only way to call it would be recursively, which is just a plain stupid thing to do, but quite possible.

In C++, calling main() was explicitly banned from C++03 and later standards:

The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation-defined. A program that declares main to be inline or static is ill-formed.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • [Related post](http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define/5296593#5296593). – Lundin Dec 11 '14 at 09:46
  • The C11 standard does say "a return from the initial call to the `main` function is [...]" which suggests one can call it in C. – philipxy Dec 11 '14 at 09:46
  • @philipxy I believe the C standard treats main just as any other function, except it has no prototype and it allows you to skip the return statement at the end. C++ is stricter and more sane. – Lundin Dec 11 '14 at 09:49
  • Updated answer with some references to C++03. – Lundin Dec 11 '14 at 09:56
  • I too can find no problems with calling it in C11. – philipxy Dec 11 '14 at 10:06
  • C++ still has the concept of a free-standing environment, where it is implementation whether a `main` function is required, as per your answer for C. – Matt Dec 11 '14 at 11:03
2

The main function is just a symbol for the entry point, and is mostly implementation defined. The C++ standard only requires implementations to accept 2 signatures, int main() and int main(int, char**).

The C++ standard does say that the function main shall not be used within a program, so it's not correct to say it can be called recursively either.

You also don't always need a main function, for example when compiling libraries you wouldn't include it.

But the answer is much simpler than all that. A function definition is where you define the function body, which for main (like any function) you define in the user code. Hence it is user-defined.

It's worth noting that User-defined function is sometimes used to refer to functions in libraries, it's not something I've heard with regards to C++, but other languages refer to UDFs as being user-submitted libraries.

Matt
  • 7,100
  • 3
  • 28
  • 58
  • The standard does not require the (hosted) implementation to accept those two forms. It can accept the first, and/or the second, and/or something completely implementation-defined. – Lundin Dec 11 '14 at 09:43
  • I guess it depends on what you mean with "the standard" though... I referred to C11, but the question is also tagged C++. I think C++ explicitly bans main() from being called, while C does not. – Lundin Dec 11 '14 at 09:50
  • @Lundin, I was referring to my copy of the C++ standard, which matches [this one](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) with regards to `main` (section 3.6.2). I've amended my answer to clarify that I'm talking about C++. – Matt Dec 11 '14 at 10:59
1

I think it is user-defined. While the signatur of the method and the naming has to follow the standard, the implementation of the function is user-defined.

That makes the function a user-defined function.

Also there are no build-in functions as far as I know in C and C++. There are some standard libraries that add library functions. But those functions are not build into the languages.

Nitram
  • 6,486
  • 2
  • 21
  • 32
0

In C++ the standard [section 3.6.1] says that main shall be the entry point (except in freestanding envs., rares!), should not be predefined, can't be overloaded and shall not be called in the program (no recursive call even indirectly).

In C, the standard defines the concept of initial call to main to define what the return value of the main is intended for. [section 5.1.2.2.3]. There is no explicit rule that forbids a recursive call to the main. There is also no predefined prototype for it even if it is common to have define it with zero or two arguments.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69