Reputation: 125
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!).
Upvotes: 0
Views: 337
Reputation: 7160
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.
Upvotes: 2
Reputation: 36441
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.
Upvotes: 0
Reputation: 215350
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.
Upvotes: 4
Reputation: 6716
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.
Upvotes: 1