开发者

In C what is the difference? [duplicate]

开发者 https://www.devze.com 2023-02-21 09:39 出处:网络
This question already has answers here: Clo开发者_运维知识库sed 11 years ago. Possible Duplicate:
This question already has answers here: Clo开发者_运维知识库sed 11 years ago.

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


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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消