2
5.1.2.2.1 Program startup
The implementation declares no prototype for this function. It shall be defined
with a return type of int and with no parameters.

我是这样定义的,

int main(int a, int b, int c){.......}

有用。我没看懂第一行"The implementation declares no prototype for this function"

需要帮助,谢谢

4

4 回答 4

4

这意味着main没有提前声明。没有像这样的线

int main(int argc, char*argv[]);

这意味着当您定义函数时,您可以假装它接受任何参数并返回您喜欢的任何类型,而不会出现编译器错误。

当然,main它是由操作系统调用的,因此它会期望您的定义与它用于传递参数的任何约定相匹配。实际上,除了在嵌入式系统上,您对 main 的定义必须与上述匹配。

于 2013-09-19T16:58:34.970 回答
2

当您制作原型时,这意味着您想在其他地方调用它,而main函数并非如此。

文档中:-

5.1.2.2.1 程序启动

1 程序启动时调用的函数名为main。实现没有声明这个函数的原型。它应定义为返回类型为 int 且不带参数:

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

或者带有两个参数(这里称为 argc 和 argv,尽管可以使用任何名称,因为它们是声明它们的函数的局部变量):

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

或等价物;9) 或以其他一些实现定义的方式。

于 2013-09-19T16:55:46.067 回答
1

declaration or prototype不需要main功能

main需要声明和定义以外的功能

int sum(int,int); //declaration
int sum(int a,int b) //definition
{
//body
}
于 2013-09-19T16:57:08.657 回答
1

您省略了标准该部分的其余引用,我将引用 C99 草案标准,该标准说:

或者带有两个参数(这里称为 argc 和 argv,尽管可以使用任何名称,因为它们是声明它们的函数的局部变量):

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

或等价物;9) 或以其他一些实现定义的方式。

引用是重要的shall be defined部分,它表示它必须遵循这两个签名之一,或者如果可用的话,一些实现特定的签名将由编译器实现者定义。

如果我尝试在最新版本中构建它,clang我会看到以下错误:

error: second parameter of 'main' (argument array) must be of type 'char **'
  int main(int a, int b, int c){}
error: third parameter of 'main' (environment) must be of type 'char **'
于 2013-09-19T16:58:16.937 回答