2

The programmer does define what happens inside main(), after all.

So, should it be considered a user-defined function?

Maxpm
  • 24,113
  • 33
  • 111
  • 170
  • If it is `user-defined function`, then I should be able to call it. So, can you call `main()` yourself in your code ? – Mahesh Jan 15 '11 at 15:40
  • @Mahesh Couldn't the same be said for library functions? – Maxpm Jan 15 '11 at 15:41
  • @Fritschy I never knew that a user can call main and just thought it to be a entry program for program execution. Just tried it - `warning C4717: 'main' : recursive on all control paths, function will cause runtime stack overflow` , Thanks for the info. – Mahesh Jan 15 '11 at 15:47
  • @Fritschy: Of what relevance is the C standard here? – Lightness Races in Orbit Jan 15 '11 at 15:48
  • 1
    @Fritschy, @Mahesh: No, you may not call `main`. See 3.6.1 in C++03. – Lightness Races in Orbit Jan 15 '11 at 15:49
  • @Mahesh Where did you call it from? Of course it will be recursive if you just put the call in `main` or even in a function that `main` calls unless you put an `if` statement somewhere. – Maxpm Jan 15 '11 at 15:50
  • @Fritschy: Evidently not, since you just asserted the opposite. I wonder who upvoted that factual error! – Lightness Races in Orbit Jan 15 '11 at 15:50
  • @Paul Definitely not a homework/interview question for me. I did do a search, but the results didn't show anything for C++. – Maxpm Jan 15 '11 at 16:08
  • @Maxpm: the same question seems to come up a lot on places like Yahoo Answers - I had always assumed it must be something from an Indian college course or something like that - maybe from one of those dreadful Kanetekar C/C++ books. – Paul R Jan 15 '11 at 16:12
  • @PaulR: What on earth does India have to do with anything? – Lightness Races in Orbit Jan 16 '11 at 15:34
  • @Tomalak: because all the Indian colleges seem to base their C/C++ courses on the Kanetkar books, and so all the bad material in these books gets propagated from one generation of Indian graduates to the next. – Paul R Jan 16 '11 at 16:11

7 Answers7

8

The C++ standard doesn't have the notion of user-defined functions. Instead, it has the notion of library functions. main is not a library function. However, the standard also imposes some requirements on its signature, and that it must not be overloaded or declared static or inline. In particular, it must not be used, meaning that you cannot call it.

Edit: I originally checked the C standard only. I have now checked the C++ standard as well, and it uses "user-defined" in the following contexts: operators, types, conversions, and libraries. It also has user-declared namespaces, user-declared (default and copy) constructors, and user-written default constructors. It does have "user functions" in 27.1.1.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
2

Yes- main is a user defined function. The easiest way to think of it would be user-defined, but Standard-declared.

It also has other restrictions, for example, non-recursive. However, on some compilers like MSVC, it's allowed to recurse in main(). I find this rather handy.

Puppy
  • 144,682
  • 38
  • 256
  • 465
1

main() is not a predefined or inbuilt function. It is a user-defined function with a predefined function prototype (also called function signature). The user writes its functionality, but its declaration has certain restrictions.

Masked Man
  • 1
  • 7
  • 40
  • 80
  • The word "template" has a specific meaning in C++, and this isn't it. And please don't use abbreviations like "u" for "you"; they just make things harder to read. – Keith Thompson Feb 23 '14 at 07:54
1

If it's not a user defined function, what would it be? Clearly not a kernel or library function? Not sure I understand what you are getting at here...

Jeff
  • 1,969
  • 3
  • 21
  • 35
1

main function is neither a built-in (predefined) nor user-defined function. It is an exception and you must follow requirements related to it which are stated in C++ standard (e.g. about its presence in program, return type and arguments).

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
0

main() is neither user-defined nor a built-in library function.

On attempting to compile a C program into an executable, the compiler looks for a function called main in your list of sources.

Creating a library (either as a shared object or by just giving out the requisite header files) however, has different ramifications.

Just so you know, GCC looks for main with either one of the following signatures:

int main(int, char **);

OR

int main();

0

main() is a predefined function from where the code execution starts. If you don't have a main function the program will not run. Hence main is the starting point of the program.

valkyrie55
  • 355
  • 4
  • 10
gargi
  • 115
  • 1
  • 4
  • 14