main()函数的返回类型 [英] The return type of main() function

查看:128
本文介绍了main()函数的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个code:

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;无效的主要(无效)
{
    字符* PTR =(字符*)malloc的(10);    如果(NULL == PTR)
    {
        的printf(\\ n malloc的失败\\ n);
        返回;
    }
    其他
    {
        //做一些处理
        免费(PTR);
    }    返回;
}

它成功地编译在Visual C,但在GCC不进行编译,我得到错误:主必须返回'诠释'。那么,main()函数的返回类型'诠释'是一个约定(这是编译器的定义),或C的规则?


解决方案

C标准(ISO / IEC 9899:2011)说:


  

5.1.2.2.1程序启动


  
  

1名为在程序启动时的功能被命名为。实施没有声明
  原型此功能。它应 INT 的返回类型,并没有被定义
  参数:

  INT主要(无效){/ * ... * /}

或两个参数(这里称为 ARGC 的argv ,虽然任何名称可能是
  使用,因为它们是局部的,其中声明它们的功能):

  INT主(INT ARGC,CHAR *的argv []){/ * ... * /}

或同等学历; 10)或一些其他实现定义的方式


  
  

10)因此, INT 可以定义为 INT 或的argv的类型可以写成
  的char ** argv的,等等。


因此​​,的唯一便携式声明的main() INT 的返回类型。如果MSVC定义了无效允许('或其他一些实现定义的方式),就这样吧,但不要指望在code可移植。旧版本的Microsoft编译器(直至并包括2005 MSVC)不允许无效的主要():看到的 :程序启动函数和程序执行。然而,2008年的MSVC,后来被记录,让无效的主要():看的 :程序启动。的三个参数的形式的main()指出,如附录J A常见的扩展:


  

J.5常用的扩展


  
  

以下扩展名被广泛应用于许多系统,但不能移植到所有
  实现。任何扩展列入可能导致严格遵循规则
  程序变得无效渲染的实施不合格。这样的例子
  扩展新的关键字,在标准头宣布额外的库函数,或
  predefined宏与不以下划线开头的名称。


  
  

J.5.1环境参数


  
  

在托管环境中,函数接收第三个参数,的char * envp []
  它指向指针的空终止数组字符,每一个指向一个字符串
  对于此执行的程序提供了有关环境的信息
  (5.1.2.2.1)。



主()传输给一个实现定义的方式对环境。

返回

价值


  

5.1.2.2.3计划终止


  
  

1如果函数的返回类型是一种与 INT 兼容,从一回
  在函数初始呼叫相当于调用退出函数值
  由函数作为参数返回; 11)达到} 终止的
  函数返回值为0如果返回类型是不符合 INT 兼容,该
  返回到主机环境的终止状态是不确定的。


  
  

11)按照6.2.4,在声明的自动存储持续时间对象的寿命
  在前者的情况下,即使在他们不会在后者具有将已结束


注意 0 的任务是为成功。您可以使用 EXIT_FAILURE EXIT_SUCCESS <文件stdlib.h> 如果您preFER,但0是公认的,所以是1参见的退出codeS大于255 - ?可能


  

7.22.4.4的退出函数


  
  

¶5最后,控制被返回到主机环境。如果状态的值为零或 EXIT_SUCCESS ,地位实现定义形式的成功终止的返回。如果状态值为 EXIT_FAILURE ,地位实现定义形式的不成功的终止返回。否则,返回的状态是实现定义的。


I get this code:

#include<stdio.h> 
#include<stdlib.h>

void main(void) 
{ 
    char *ptr = (char*)malloc(10); 

    if(NULL == ptr) 
    { 
        printf("\n Malloc failed \n"); 
        return; 
    } 
    else 
    { 
        // Do some processing 
        free(ptr); 
    } 

    return; 
} 

It compiles successfully in Visual C, but do not compile in gcc, I get "error:'main' must return 'int'". So is the return type 'int' of main() function is a convention(which is for compiler to define), or a rule of C?

解决方案

The C standard (ISO/IEC 9899:2011) says:

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.

Thus, the only portable declaration for main() has a return type of int. If MSVC defines that void is permitted ('or in some other implementation-defined manner'), so be it, but do not expect the code to be portable. The old versions of the Microsoft compilers (up to and including MSVC 2005) do not permit void main(): see the documentation at main: Program startup and The main Function and Program Execution. However, MSVC 2008 and later are documented to allow void main(): see main: Program Startup. The three-argument form of main() is noted as a common extension in Appendix J:

J.5 Common extensions

The following extensions are widely used in many systems, but are not portable to all implementations. The inclusion of any extension that may cause a strictly conforming program to become invalid renders an implementation nonconforming. Examples of such extensions are new keywords, extra library functions declared in standard headers, or predefined macros with names that do not begin with an underscore.

J.5.1 Environment arguments

In a hosted environment, the main function receives a third argument, char *envp[], that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.2.1).


The value returned from main() is transmitted to the 'environment' in an implementation-defined way.

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.

Note that 0 is mandated as 'success'. You can use EXIT_FAILURE and EXIT_SUCCESS from <stdlib.h> if you prefer, but 0 is well established, and so is 1. See also Exit codes greater than 255 — possible?.

7.22.4.4 The exit function

¶5 Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

这篇关于main()函数的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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