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

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

问题描述

我刚刚碰到别人的C code,它我很困惑,为什么它正在编制。有两点我不明白。

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.

首先,函数原型相比,实际的函数定义没有参数。第二,在函数定义的参数没有一个类型

First, the function prototype has no parameters compared to the actual function definition. Second, the parameter in the function definition does not have a type.

#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

一个功能被以下面的方式宣称:

A function is declared in the following manner:

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


  
  

返回式是变量类型的函数返回。这不可能是一个数组型或函数型。 如果没有给出,则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.

函数名称是函数的名称。

参数列表是该功能需要用逗号分隔的参数列表。 若没有给定参数,那么函数
  不采取任何,应与空集的定义
  括号或关键字void。如果没有变量类型在前面
  在paramater列表中的变量,那么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)

使用函数声明与空括号(不
  原型格式参数类型说明符)的是一个过时
  功能

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

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