返回主要类型()! ! [英] Return type of main() ! !

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

问题描述

这可以从main()函数返回void,float,double,char,指针类型数据吗?



Is this possible to return void, float, double, char, pointer type data from main() function?

#include<stdio.h>
double main(){
printf("No may be not!!");
return 0.0;
}




#include<stdio.h>
float main(){
printf("No may be not!!");
return 0.0;
}







#include<stdio.h>
 *main(){
printf("No may be not!!");
int n=9;
int *a;
a=n;
return (&a);
}







#include<stdio.h>
char main(){
printf("No may be not!!");
return '\0';
}





我的尝试:



我希望它们在codeblock中编译,虽然我收到错误消息。为什么?不能像上面那样主要返回任何内容吗?



What I have tried:

I want them compiled in codeblock though I got error message. Why? Can't main return anything as above?

推荐答案

main()是一个特殊功能,按照惯例将整数值返回给主机操作系统。
main() is a special function and by convention it returns an integer value to the host OS.


因为 main 必须返回一个int。

参见 ISO / IEC 9899 (C99)和 ISO / IEC 9899:201x (C11)§ 5.1.2.2.1程序启动

Because main have to return an int.
See ISO/IEC 9899 (C99) and ISO/IEC 9899:201x (C11) at §5.1.2.2.1 Program startup:
Quote:



程序启动时调用的函数是命名主要。该实现为此函数声明没有

原型。它应定义为返回类型 int 且没有

参数:

int main(void){/ *。 .. * /}

或带有两个参数(这里称为argc和argv,但任何名称可能都是使用
,因为它们是本地的它们被声明的函数):

int main(int argc,char * argv []){/ * ... * /}

或等价物,或者其他一些实现定义的方式。


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, or in some other implementation-defined manner.



你可以使用2个参数定义main int argc char * argv [] ,或者什么也没有。但是根据所有现行标准,回报不能无效

无论如何,一些编译器允许形式 void main(void){/ * ... * /} 作为私有扩展不符合标准


You can define main with the 2 parameters int argc and char *argv[], or nothing. But the returns, as per all current standards, cannot be void.
Anyway some compilers allow the form void main(void) { /* ... */ } as private extension not standard compliant.


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

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