-2

What is the difference between main, void main and int main in C? what does each one of them do? Also what is return 0; used for? i know it somehow tells the OS that the program finished succesfully but what does that have to offer? I would like to note that ive been working on C for a little more than a month so im not really experienced

JohnEm
  • 29
  • 5

3 Answers3

1
5.1.2.2.1 Program startup

1 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[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.
10) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.

C 2011 Online Draft

main() is equivalent to int main(void). Under earlier versions of the language, if you defined a function without an explicit return type, the compiler assumed it returned int. Also, if you define a function without any parameters, that means the function takes no parameters. Implicit typing is no longer allowed, and using prototype syntax allows you to catch errors in the number and types of arguments at compile time, so this form should no longer be used.

void main() is not standard, and should not be used unless your implementation explicitly documents it as a valid signature for main ("or in some other implementation-defined manner")1. Otherwise, using it results in undefined behavior, which may result in your code misbehaving on startup or exit. There are platforms where it runs with no apparent issues, but you shouldn't rely on that being true.

5.1.2.2.3 Program termination

1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;11) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
11) In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in main will have ended in the former case, even where they would not have in the latter.

C programs return a status code to the runtime environment - on *nix and similar platforms, a return code of 0 indicates successful, normal program termination. stdlib.h defines the macros EXIT_SUCCESS and EXIT_FAILURE, which should be used instead of literal numeric values:

#include <stdlib.h>
...
int main( void )
{
  ...
  if ( something_bad_happens )
    return EXIT_FAILURE;
  ...
  return EXIT_SUCCESS;
}


  1. Even then, I wouldn't use it, because it's guaranteed to be non-portable.

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

The difference between them is that void main() is not a valid signature for the main function according to the standard. According to the standard, there are only two valid signatures, and those are:

  • int main(void)
  • int main(int argc, char *argv[])

A compiler may allow other signatures, but it is not required by the standard. One common non-standard signature is:

  • int main(int argc, char *argv[], char *env[])

The return statement simply returns an integer that can give information to the caller. If you're in a linux environment, you can inspect this value like this:

$ ./a.out
$ echo $?

In the bash shell, the variable $? contains the return value of the last exectuded command.

You can read more about main signatures and return values here: https://port70.net/~nsz/c/c11/n1570.html#5.1.2

klutt
  • 30,332
  • 17
  • 55
  • 95
-1

int main is the only valid way to declare the main() function, the others are wrong. You can optionally specify parameters int main(int argc, char *argv[]), but the return type int is required.

The return value of the main() function is used to tell the calling application how the program ended. Returning 0 means that the program completed successfully, while non-zero values are used to indicate some kind of error. This is typically tested in shell scripts. You can also call the exit() function to end the program, its argument will be used as the exit status the same way (this allows terminating the program from inside functions).

Barmar
  • 741,623
  • 53
  • 500
  • 612