gpt4 book ai didi

c - 为什么没有参数的函数(与实际函数定义相比)可以编译?

转载 作者:行者123 更新时间:2023-11-30 16:42:05 24 4
gpt4 key购买 nike

我刚刚看到某人的 C 代码,我很困惑为什么要编译它。有两点不太明白。

  1. 与实际函数定义相比,函数原型(prototype)没有参数。

  2. 函数定义中的参数没有类型。

<小时/>
#include <stdio.h>

int func();

int func(param)
{
return param;
}

int main()
{
int bla = func(10);
printf("%d", bla);
}

为什么这有效?我已经在几个编译器中测试了它,它工作得很好。

最佳答案

所有其他答案都是正确的,但仅限于 completion

A function is declared in the following manner:

  return-type function-name(parameter-list,...) { body... }

return-type is the variable type that the function returns. This can not be an array type or a function type. If not given, then int is assumed.

function-name is the name of the function.

parameter-list is the list of parameters that the function takes separated by commas. If no parameters are given, then the function does not take any and should be defined with an empty set of parenthesis or with the keyword void. If no variable type is in front of a variable in the paramater list, then int is assumed. Arrays and functions are not passed to functions, but are automatically converted to pointers. If the list is terminated with an ellipsis (,...), then there is no set number of parameters. Note: the header stdarg.h can be used to access arguments when using an ellipsis.

为了完整起见,再次强调。 From C11 specification 6:11:6 (第179页)

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

关于c - 为什么没有参数的函数(与实际函数定义相比)可以编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46041337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com