为什么你会precede C中的main()函数与数据类型? [英] Why would you precede the main() function in C with a data type?

查看:229
本文介绍了为什么你会precede C中的main()函数与数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多人熟悉C的Hello World程序

Many are familiar with the hello world program in C

#include <stdio.h>

main ()
{
    printf ("hello world");
    return 0;

}

为什么有些precede的main()函数与诠释为:

Why do some precede the main () function with int as in:

int main()

另外,我已经看到了这个词无效里面输入了(),如:

Also, I've seen the word 'void' entered inside the () as in:

int main(void)

好像白白额外的输入,但也许它是支付股息在其他情况下的最佳做法?

It seems like extra typing for nothing, but maybe it's a best practice that pays dividends in other situations?

另外,为什么precede的main()用,如果你返回一个字符串一个int?如果有的话,人们所期望的:

Also, why precede main() with an int if you're returning a character string? If anything, one would expect:

char main(void)

我也云里雾里,为什么我们在函数的最后返回0。

I'm also foggy about why we return 0 at the end of the function.

推荐答案

下面一直在有效C89

The following has been valid in C89

main() {
  return 0;
}

但在现代C(C99),这不是了,因为你需要明确地告诉变量的类型并返回的功能类型允许的,因此它成为

But in modern C (C99), this isn't allowed anymore because you need to explicitly tell the type of variables and return type of functions, so it becomes

int main() {
  return 0;
}

此外,这是合法的省略 0的回报在现代的C ,所以它是合法的写

int main() {

}

和行为是因为如果它返回0。

And the behavior is as if it returned 0.

人们把无效括号中,因为它确保函数调用正确的类型检查。 C中的空括号意味着没有对参数的数量和类型等信息的功能外暴露,并且调用者必须确切地知道这些。

People put void between the parentheses because it ensures proper typechecking for function calls. An empty set of parentheses in C mean that no information about the amount and type of the parameters are exposed outside of the function, and the caller has to exactly know these.

void f();
 /* defined as  void f(int a) { } later in a different unit */

int main() {
  f("foo");
}

˚F到的调用导致未定义的行为,因为编译器无法验证反对什么˚F预计,在其他模块。如果你把它与无效 INT ,编译器会知道写

The call to f causes undefined behavior, because the compiler can't verify the type of the argument against what f expects in the other modules. If you were to write it with void or with int, the compiler would know

void f(int); /* only int arguments accepted! */

int main(void) {
  f("foo"); /* 'char*' isn't 'int'! */
}

因此​​,对于它只是把一个好习惯无效有,因为这是很好的在其他地方做。在C语言中你被允许递归调用在这种情况下,这种差异甚至可能关系。

So for main it's just a good habit to put void there since it's good to do it elsewhere. In C you are allowed to recursively call main in which case such differences may even matter.

不幸的是,只有少数编译器支持现代的C,所以很多C编译器,你还可以得到警告,用现代的C功能。

Sadly, only a few compilers support modern C, so on many C compilers you may still get warnings for using modern C features.

此外,您可能会看到程序申报返回比 INT 不同的东西。这种方案可以做到这一点,如果他们使用的独立的C实现。这样的C实现勿施于任何限制主,因为他们不知道,或者需要摆在首位这样的功能。但有一个共同的和可移植的接口程序的入口点时,C规范要求严格符合程序申报主返回类型为 INT ,并且需要的托管 C实现接受这种方案。

Also, you may see programs to declare main to return different things than int. Such programs can do that if they use a freestanding C implementation. Such C implementations do not impose any restrictions on main since they don't even know or require such a function in the first place. But to have a common and portable interface to the program's entry point, the C specification requires strictly conforming programs to declare main with return type int, and require hosted C implementations to accept such programs.

这篇关于为什么你会precede C中的main()函数与数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆