为什么没有参数的函数(与实际函数定义相比)编译? [英] Why does a function with no parameters (compared to the actual function definition) compile?

查看:31
本文介绍了为什么没有参数的函数(与实际函数定义相比)编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了某人的 C 代码,我对编译的原因感到困惑.有两点不明白.

I've just come across someone's C code that I'm confused as to why it is compiling. There are two points I don't understand.

  1. 函数原型与实际函数定义相比没有参数.

  1. The function prototype has no parameters compared to the actual function definition.

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


#include <stdio.h>

int func();

int func(param)
{
    return param;
}

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

为什么会这样?我已经在几个编译器中对其进行了测试,并且运行良好.

Why does this work? I have tested it in a couple of compilers, and it works fine.

推荐答案

所有其他答案都是正确的,但仅针对 补全

All the other answers are correct, but just for completion

函数的声明方式如下:

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

return-type 是函数返回的变量类型.这不能是数组类型或函数类型.如果没有给出,则int假设.

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 是函数的名称.

parameter-list 是函数采用的参数列表,以逗号分隔.如果没有给定参数,则函数不接受任何,应该用一个空集定义括号或关键字 void.如果前面没有变量类型参数列表中的变量,则假定为 int.数组和函数不会传递给函数,而是自动转换到指针.如果列表以省略号 (,...) 结尾,则没有固定数量的参数.注意:头文件 stdarg.h 可以是用于在使用省略号时访问参数.

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.

再次为完整性起见.来自 C11 规范 6:11:6(第 179 页)

And again for the sake of completeness. From C11 specification 6:11:6 (page: 179)

使用带空括号的函数声明符(不是原型格式参数类型声明符)已过时功能.

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

这篇关于为什么没有参数的函数(与实际函数定义相比)编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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