1

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

Example1

#include <stdio.h>

int main(void)
{
      printf ("Hello World!\n");
      getch();
}

Example2

#include <stdio.h>
void main()
{
     printf ("Hello World!\n");
     getch();
}

Both output:

Hello World
Community
  • 1
  • 1
tree em
  • 20,379
  • 30
  • 92
  • 130

3 Answers3

4

The first example is recommended but also you need to put this line as the last one in the main()

return 0;

which returns zero to the system indicating that everything went alright.

Nabeel
  • 557
  • 4
  • 15
2

1. main() return type

- For managed applications:

In managed environments, such as an operating system, each application should return it's status to the OS on exit. For example in Unix, application will return 0 on success and some non-zero value in case of an error occurred.

To do that in your application, you should specify that main() function will return integer: int main()

So, you normally would write:

#include <stdio.h>

int main(void)
{
      printf ("Hello World!\n");
      getch();

      return 0;
}

However, (as pointed out already) return 0; is optional, and compiler will automatically add this statement in the end of main() function if main() is specified to return int value.

- For non-managed applications:

Non-managed applications, ones that are not managed by OS or some kind of environment, do not need (and cannot) report it's status, because there is nowhere to report.

Examples of such non-managed application would be:

  • application in small embedded systems, where there is usually no OS, and there is only one application running (so it has nowhere to report).
  • OS kernel itself - it is the top level application that is the managing environment itself.

Moreover, these applications usually never exit and run in the loop until the power is shut down.

In these cases (although sometimes non-managed applications don't have main() at all), void should be the return type of main() function:

#include <stdio.h>

void main(void)
{
      printf ("Hello World!\n");

      while (1);
}

2. main() arguments

In managed environments application can not only return it's status on exit, it can also input some parameters on launch.

For example, in Unix you would use GCC compiler with some parameters:

gcc -std=c99 -pedantic hello.c -o /tmp/hello

in this case we launch GCC with some textual parameters such as -std=c99 wich sais that the program should be compiled according to C99 standard.

To be able to input parameters in your program you can declare:

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

You can read here more about it.

However, if you don't want to input any arguments, you can leave it void:

int main(void)

3. Dropped out types (implicit type declaration)

Sometimes you can find programs where input or output types are not declared at all:

int main()

or

main(int argc, char *argv[])

or even just

main()

This is very sloppy programming style and it is strongly discouraged in C. In case there is no input arguments, or function does not return any value you should explicitly specify void. If you don't specify any type for function or arguments, compilers will always assume int. That means that main() will be transformed by compiler to int main().

(int is the default type in C, because language designers, Kernighan or Ritchie assumed that it will be the most used type, and decided to allow to dropp type declaration in case of ints)

Vladimir Keleshev
  • 13,753
  • 17
  • 64
  • 93
  • "Usual" does not mean "standard compliant". Please don't advocate writing invalid code. – Lightness Races in Orbit Apr 03 '11 at 00:07
  • `foo()` does not mean `foo(int)` -- that's quite wrong. – Jim Balter Apr 03 '11 at 03:16
  • @Tomalak Geret'kal, if you mean `void main(void)` - this is absolutely standard-compliant code for embedded systems with no OS (and for any non-managed application). Take a look here: http://users.ece.utexas.edu/~valvano/embed/chap1/chap1.htm – Vladimir Keleshev Apr 03 '11 at 09:17
  • @Jim Balter - yeah, you're right, `foo(int)` does not make sense, however `foo()` will be transformed to `int foo()`, and `foo(var)` will be transformed to `int foo(int var)` (not the best practice though). – Vladimir Keleshev Apr 03 '11 at 09:21
  • @Halst: Just because software on an embedded system may not quit does not change the ANSI C standard. Not only does that website _not_ say anywhere that `void main(void)` is standard-compliant, but it also wouldn't be authoritative if it did. In fact, if you read the ANSI C standard, you'll see that it's not. – Lightness Races in Orbit Apr 03 '11 at 13:58
  • @Tomalak Geret'kal, in ISO/IEC 9899:TC 5.1.2.2.3 Program termination, it is specified that if the return type of `main()` is not compatible with `int`, the termination status returned to the hosting environment is unspecified - that is exactly the case for embedded systems. – Vladimir Keleshev Apr 03 '11 at 18:13
  • @Halst: Yes, it's unspecified. Because the entrypoint function is [supposed](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-c/207992#207992) to have a return type of `int`. Your "embedded systems" (that's very vague, btw; my embedded system is strictly conformant) work but are not strictly conformant. – Lightness Races in Orbit Apr 03 '11 at 18:49
  • @Tomalak, to put an end to this discussion I suppose you should see section 5.1.2.1: `In a freestanding environment […], the name and type of the function called at program startup are implementation-defined.` – Vladimir Keleshev Apr 05 '11 at 20:10
  • @Halst `int foo(int);` is a perfectly valid declaration; it just isn't equivalent to `int foo();`, leaves the number of parameters and their types unspecified. And your last paragraph remains incorrect -- when C was first designed, ints and pointers were interchangeable, so leaving the type off of a function declaration allowed you to return anything that could be held in an int. – Jim Balter Apr 06 '11 at 10:05
0

Both code example will have the same behavior once it is compiled.

The first example is more close to standard C 89. It's just missing a return statement (that is optional in C 99).

Compiler may start complaining if you chose a strict compliance to C 89.

greydet
  • 5,509
  • 3
  • 31
  • 51
  • 2
    No version is C89 conformant, even ignoring `getch`. Implicit `return 0;` at the end of `main` was added in C99. – pmg Apr 02 '11 at 22:22
  • @pmg you are right I did not mention the missing return statement. I was focused on differences between the two code examples. – greydet Apr 02 '11 at 22:38