-9

Possible Duplicate:
What should main() return in C/C++?

What value does this function return. just plain main.

main()
{
...
}

and if a function has two mains , what happens?

Community
  • 1
  • 1

3 Answers3

4

What value does this function return.

main needs to be declared as returning an int. The return value is passed to the caller, which is usually the operating system.

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:

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

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

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

and if a function has two mains , what happens?

Linker reports an error.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

In C99/C11, main returns 0 if the } is reached in a hosted environment,. Else, the return value is undefined.

C11, § 5.1.2.2.2 Program execution

[...] reaching the } that terminates the main function returns a value of 0.

Community
  • 1
  • 1
md5
  • 23,373
  • 3
  • 44
  • 93
0

Assuming you're using a C89 or earlier compiler, then

main()
{
  ...
}

returns int. If you're using a C99 or later compiler, it's an error.

As of C99, if you reach the ending } of main without an explicit return, the return value is 0. Not sure about C89 or earlier.

Not sure what "a function has two mains" is supposed to mean. If a program has two main functions defined, then you'll most likely get a duplicate definition error at link time.

John Bode
  • 119,563
  • 19
  • 122
  • 198