这是间diferrence:主(){}和INT主(){}和INT主(无效){} [英] which is the diferrence between: main(){} and int main(){} and int main(void){}

查看:125
本文介绍了这是间diferrence:主(){}和INT主(){}和INT主(无效){}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习C,现在直到我想出了许多小程序。但是我注意到,在主函数其中不乏这样开始:

I am learning now c, and till now I have come up with many small programs. However i have noticed that many of them in the main function start like:

    main()
    {
       //code
    }

    int main()
    {
      //code
      return 0;
    }

    int main(void)
    {
       //code
       return 0;
    }

所以,我应该用什么?或遵循的最佳方式?感谢名单!

So, what should i use? or which the best way to follow? thanx!

推荐答案

您第一个示例使用从C过时的方言predated第一ANSI(1989)和ISO(1990)标准继承了一个功能,即:你可以写一个不指定它的返回类型,在这种情况下,类型默认为 INT 的功能。

Your first example uses a feature inherited from the outdated dialect of C which predated the first ANSI(1989) and ISO(1990) standard: namely, that you can write a function which doesn't specify its return type, and in that case the type defaults to int.

在C月初,无效关键字和相关联的类型不存在。当程序员想写程序(有副作用,但不返回任何功能),他们使用此功能模拟它。他们写了一个函数,不需要任何关键字指定返回类型。他们允许该函数不返回值执行它的最后一条语句(或者,他们用返回; 从中间退出而不提供一个值),他们写的调用等功能,这些电话没有尝试使用返回值:

In early C, the void keyword and associated type did not exist. When programmers wanted to write procedures ("functions that have a side effect, but do not return anything"), they simulated it using this feature. They wrote a function without any keyword specifying the return type. They allowed the function to execute to it last statement without returning a value (or alternatively, they used return; to exit from the middle without supplying a value), and they wrote the calls to the function such that those calls did not try to use the return value:

parse_input() /* similar to a procedure in Pascal, but fake! */
{
   /* ... */
   if (condition())
     return; /* no value */
   /* ... */
   /* fall off end here */
}

int main()
{
   parse_input(); /* no return value extracted, everything cool! */
   return 0;
}

不幸的是,有些程序员也开始不关心程序的终止状态,写本身在此过程中的风格:

Unfortunately, some programmers also started not caring about the termination status of a program and writing main itself in this procedure style:

main()
{
  /* do something */
  /* fall off the end without returning a value */
}

(混合风格也存在:省略 INT 声明符,但返回一个整数值)

(A mixed style also existed: omitting the int declarator but returning an integer value.)

这些计划未能返回值有一个不确定的终止状态。到操作系统,它们的执行可能看起来成功或失败。的祸剧本作家谁试图依靠这样一个程序的终止状态!

These programs failing to return a value had an indeterminate termination status. To the operating system, their execution could look successful or failed. Woe to the script writer who tried to depend on the termination status of such a program!

然后把事情的急转直下。 C ++来了,并推出无效,并通过了为C.与C ++中的无效关键词,人们可以申报实际上没有返回值的函数(并使其错误有一个返回; 在任何其它类型的函数语句)。伪程序员谁用来写没有返回类型得到了笨,并开始坚持这一新奇,新鲜外的C ++ 无效在前面:

Then things took a turn for the worse. C++ came along and introduced void, and it was adopted into C. With the void keyword in C++, one could declare a function that actually returns nothing (and make it an error to have a return; statement in any other kind of function). The dummy programmers who used to write main with no return type got dumber, and started sticking this new-fangled, fresh-out-of-C++ void in front:

void main() /* yikes! */
{
  /* do something */
  /* fall off the end without returning a value */
}

这个时候,他们忘记了,当他们写了的main(),它实际上意味着 INT的main() ,这使得该函数有一个兼容型与环境(除忽略返回值的物质)调用启动呼叫。现在,他们实际上有从所预期的,这甚至可能不会成功调用不同的功能类型!

By this time they had forgotten that when they wrote main(), it actually meant int main(), which made the function have a compatible type with the startup call invoked by the environment (except for the matter of neglecting to return a value). Now they actually had a different function type from the expected one, which might not even be successfully called!

在哪里的东西现在忍受的是,在C ++和最新的C ++标准,仍需要返回一个 INT 。但是,这两种语言作原始虚拟程序员的让步:可以让执行力脱落的和行为就如同符结束0; 已经在那里执行。所以,这个简单的程序现在有一个成功的终止状态为C99的,并且我认为,C ++ 98(或可能更早):

Where things stand now is that in C++ and in the latest C++ standard, main is still required to return an int. But both languages make a concession for the original dummy programmers: you can let execution "fall off" the end of main and the behavior is as if return 0; had been executed there. So this trivial program now has a successful termination status as of C99 and, I think, C++98 (or possibly earlier):

int main()
{
}

但无论是语言使得第二代笨程序员(和其他人谁读的C语言的书籍,那些程序员在20世纪80年代,自写)的让步。也就是说,无效不是一个有效的返回声明符主(除非它是由平台为被接受记载,而且仅适用于这些平台,不向便携式语言)。

But neither language makes a concession for the second-generation dumber programmers (and everyone else who read the C books that those programmers wrote in the 1980's and since). That is, void is not a valid return declarator for main (except where it is documented by platforms as being accepted, and that applies to those platforms only, not to the portable language).

哦,是从C在C99去除津贴缺少声明符,那么的main(){} 不再正确在C新方言,和ISN ŧ有效的C ++。顺便说一句,C ++确实有这样的语法在别处:即,需要类的构造函数和析构函数没有返回类型说明符。

Oh, and allowance for the missing declarator was removed from C in C99, so main() { } is no longer correct in new dialects of C, and isn't valid C++. Incidentally, C++ does have such a syntax elsewhere: namely, class constructors and destructors are required not to have a return type specifier.

好吧,现在约()(无效)。回想一下,C ++引入了无效。此外,虽然C ++引入了无效,它并没有引入(无效)参数语法。 C ++被更严格地键入推出原型声明,并驱逐了unprototyped函数的概念。 C ++改变了() C语法的意义给它声明的权力。在C ++中, INT FUNC(); 声明不带参数的函数,而在C, INT FUNC(); 不会做这样的事情:它声明的函数这是我们不知道的参数信息。当C采用无效,该委员会有一个丑陋的想法:我们为什么不使用语法(无效)来声明函数不带参数,然后在()语法可以留在loosey-说傻话传统行为迎合无类型节目向后兼容。

Okay, now about () versus (void). Recall that C++ introduced void. Furthermore, though C++ introduced void, it did not introduce the (void) argument syntax. C++ being more rigidly typed introduced prototype declarations, and banished the concept of an unprototyped function. C++ changed the meaning of the () C syntax to give it the power to declare. In C++, int func(); declares a function with no arguments, whereas in C, int func(); doesn't do such a thing: it declares a function about which we do not know the argument information. When C adopted void, the committee had an ugly idea: why don't we use the syntax (void) to declare a function with no arguments and then the () syntax can stay backward compatible with the loosey-goosey legacy behavior pandering to typeless programming.

您可以猜到接下来发生的事:C ++的人看着这个(无效)劈,吐了他们的武器并将它复制到C ++的跨语言的缘故兼容性。当你在看语言是如何分道扬镳的今天,基本不再关心兼容性到何种程度上在事后是惊人的。因此,(无效) unambiguosly意思是宣告为无参数,在C和C ++。但在C ++ code。使用它是纯显然C ++从未打算成为C是丑,风格差:例如,在类的成员函数!它没有多大意义,写东西像类Foo {市民:美孚(无效);虚拟〜美孚(无效)/*...*/};

You can guess what happened next: the C++ people looked at this (void) hack, threw up their arms and copied it into C++ for the sake of cross-language compatibility. Which in hindsight is amazing when you look at how the languages have diverged today and basically no longer care about compatibility to that extent. So (void) unambiguosly means "declare as having no arguments", in both C and C++. But using it in C++ code that is obviously pure C++ never intended to be C is ugly, and poor style: for instance, on class member functions! It doesn't make much sense to write things like class Foo { public: Foo(void); virtual ~Foo(void) /*...*/ };

当然,当你的确定 A函数int的main(){...} ,其定义函数没有参数不论哪一种语言的是在,所不同的是在什么声明信息被引入到的范围。在C,我们可以有荒谬的情况,一个功能可完全定义,但没有申报,程序文本的同一单位!

Of course, when you define a function like int main() { ... }, the function which is defined has no arguments, regardless of which language it is in. The difference is in what declaration info is introduced into the scope. In C we can have the absurd situation that a function can be fully defined, and yet not declared, in the same unit of program text!

当我们写,通常它不是从程序中调用,所以它没有的无论的是什么的定义声明。 (在C ++中,不得从程序中调用;在C中,它都可以)。因此,它是无形的,你是否写 INT的main() INT主要(无效),无论你是使用C或C ++。它调用的东西没有看到它的任何声明(你在你的程序写,反正)。

When we write main, usually it is not called from within the program, and so it doesn't matter what the definition declares. (In C++, main must not be called from the program; in C it can be). So it is immaterial whether you write int main() or int main(void), regardless of whether you're using C or C++. The thing which calls main does not see any declaration of it (that you write in your program, anyway).

因此​​,只要记住,如果你写的:

So just keep in mind that if you write:

int main()  /* rather than main(void) */
{ 
}

话虽然它是完美的C ++和正确的C,因为C时有轻微瑕疵的风格:你写一个老风格pre-ANSI-C函数不作为原型。虽然它并不功能此事的主力的情况下,如果你以某种方式使用一些编译器你可能会得到一个警告。例如,海湾合作委员会,以 -Wstrict的原型选项:

then although it is perfect C++ and correct C, as C it has a slight stylistic blemish: you're writing an old-style pre-ANSI-C function that doesn't serve as a prototype. Though it doesn't functionally matter in the case of main, you may get a warning if you use some compilers in a certain way. For instance, GCC, with the -Wstrict-prototypes option:

test.c:1:5: warning: function declaration isn’t a prototype [-Wstrict-prototypes]

由于 -Wstrict的原型是一个混账有用的警告,把在C语言进行编程时,为了提高类型安全(与 -Wmissing沿-prototypes ),并且我们努力消除我们的编译工作的警告,我们有必要写:

Because -Wstrict-prototypes is a darn useful warning to turn on when programming in C, for improved type safety, (along with -Wmissing-prototypes), and we strive to eliminate warnings from our compile jobs, it behooves us to write:

int main(void) /* modern C definition which prototypes the function */
{
}

这将使该诊断走了。

which will make that diagnostic go away.

如果你想来接受参数,那么它是 INT主(INT ARGC,字符** argv的)其中,参数名称的给你。

If you want main to accept arguments, then it is int main(int argc, char **argv) where the parameter names are up to you.

在C ++中,你可以省略参数名称,所以这个定义是可能的,这在这个地方很好地提供的main()

In C++, you can omit parameter names, so this definition is possible, which serves nicely in the place of main().

int main(int, char **) // both arguments ignored: C++ only
{
}

由于参数向量是空指针结尾的,你不需要 ARGC 和C ++,让我们的前preSS,没有引入一个未使用的变量:

Since the argument vector is null-pointer-terminated, you don't need argc, and C++ lets us express that without introducing an unused variable:

#include <cstdio>

int main(int, char **argv)  // omitted param name: C++ only
{
  // dump the arguments
  while (*argv)
    std::puts(*argv++);
}

这篇关于这是间diferrence:主(){}和INT主(){}和INT主(无效){}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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