-5

In every C program I had seen, there will be the main() function. Unlike in other languages, such as Java and C++, it has no data type before it. In C++ and Java there will at least be a void with main(), if not some other data type.

Why does main() have no data type with it in C?

Also, in Java the constructor has no data type used with it. Is it possible to return any value if no data type is used before functions? Or by default, can they return any value?

Adi Inbar
  • 10,985
  • 13
  • 49
  • 65
TomJ
  • 1,655
  • 13
  • 35
  • 54
  • see http://msdn.microsoft.com/en-us/library/f9t8842e.aspx, http://bytes.com/topic/c/answers/690083-changing-entry-point-function – IdeaHat Apr 24 '14 at 14:48
  • Duplicate http://stackoverflow.com/questions/3549879/is-main-a-pre-defined-function-in-c?rq=1 – Casper Beyer Apr 24 '14 at 14:51
  • 1
    Your question is essentially "I see the main function defined in source code that is not in any library; is defined in a library?" I am interested to learn what causes people to have self-contradictory beliefs about programs, because that helps me designed tools to help them. Can you explain why you believed that code that you observe to not be in any library might be in a library? – Eric Lippert Apr 24 '14 at 14:54
  • 1
    This is not a great question, but the number of downvotes is pretty extreme. – Fred Foo Apr 24 '14 at 14:55
  • you're probably confused because there is a compiler defined function called `_start()` which is actually the entry point for your program. it calls your `main` function. This is why sometimes you see the error `function '_start':: undefined reference to 'main'` if you leave out `main` for some reason. – Steve Cox Apr 24 '14 at 14:56
  • also, while main is user defined, it is definitely library declared, otherwise the C implementation wouldn't know how to call it – Steve Cox Apr 24 '14 at 15:07
  • 1
    I think this should be reopened and then marked as a duplicate (which is functionally *not* the same as closing, and arguably should be separated from close votes). The answers are essentially the same, but the question asked here is different in a way that's useful for searchability. – Adi Inbar Apr 25 '14 at 02:23
  • 1
    @SteveCox: it's not declared, see the answer by @haccks. In fact it can't be (at least not with a prototype), because both `int main(int, char**)` and `int main(void)` are accepted and C has no overloading. – Fred Foo Apr 25 '14 at 12:45

4 Answers4

4

It's user-defined: you define it in each program. If it were a library function, you wouldn't define it, because the library would define it for you.

(As an exception to this, the Unix libraries libl and liby, the support libraries for Lex and Yacc, actually define main; the idea is that you define a bunch of functions with standard names and you get a default main that calls these functions. You can override their main, though. Apparently Windows compilers do something similar.)

Fred Foo
  • 328,932
  • 68
  • 689
  • 800
1

main() is a user-defined function.

pmg
  • 98,431
  • 10
  • 118
  • 189
1

Standard clearly states that it is a user defined function:

C11: 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:....

haccks
  • 97,141
  • 23
  • 153
  • 244
  • 1
    This does not imply it's never a library function. A conforming environment could provide `main` in a library and expect the user to define some other entry point that is called by this function. See my answer for examples. – Fred Foo Apr 24 '14 at 14:54
  • I have no idea about what you explained in your answer. But it is interesting to know! – haccks Apr 24 '14 at 14:59
1

main() is always user defined, and can be prototyped in many ways (although some will argue that point)

(edited)

main(void){/*User defined content*/} // K&R implied `int` return type
void main(void){/*User defined content*/}  //yeah, yeah, I know
int main(void){/*User defined content*/ return 0;} //commonly used 
int main(int argc, char *argv[]){/*User defined content*/ return 0;} //commonly used 

I use a C99 implementation of a C compiler. While it very explicitly notifies of any warning or error for illegal or nefarious syntax, It did not flag the following scenario:

enter image description here

So, while it is not strictly conforming, it is evidently not illegal.

ryyker
  • 20,437
  • 3
  • 35
  • 78
  • 2
    `void main(void)` should not be on this list. http://stackoverflow.com/a/207992/10077 – Fred Larson Apr 24 '14 at 15:19
  • @FredLarson - Lol! (anticipated your comment). I personally do not use it, but I put it there because it compiles, and because the standard is less pedantic on the topic than the people that complain about it. The C99 standard ***does*** allow for other implementation-defined signatures, and you can use these if you've read the manual for your compiler and it says you can. (5.1.2.2.1) _It shall be defined with a return type of int and with no parameters ... or with two parameters ..._ ***or in some other implementation-defined manner*** (http://stackoverflow.com/a/9356660/645128) – ryyker Apr 24 '14 at 17:19
  • But it's never standard, only implementation defined for certain compilers. Now you've added one that omits `int` (relying on implicit `int`), which is legal in C90 but not C99. – Fred Larson Apr 24 '14 at 17:31
  • @FredLarson - again, I am not promoting void main(void), and I agree it is non-conforming, but if I can compile and build in a C99 compiler, it is evidently not _illegal_. (see edit for illustration if you are interested). ***The same is true*** for the `main(void){...} prototype, where int return type is implied: My C99 environment allows it, the C99 standard makes provision for it, so, although non-conforming, it is _not illegal_ – ryyker Apr 26 '14 at 15:30