空参数列表是什么意思? [英] What does an empty parameter list mean?

查看:36
本文介绍了空参数列表是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读的这本书指出,当你在 c 中声明一个不接受任何参数的函数,但你没有使用关键字 void 函数调用可以传递任何它想要的参数".所以我尝试了这个.

The book that i am currently reading states that when you declare a function in c that accepts no arguments, but you do not use the keyword void "the function call can pass any arguments it wants". so i attempted this.

int number();

int main(void)
{
    int x =1;
    printf("%d",number(x));
}

int number()
{
    return x;
}

但它没有编译???这是如何工作的?

but it did not compile??? how does this work?

推荐答案

这是几十年前 C 标准化之前的一个过时特性1.

This is an obsolescent feature1 from before C was standardized, decades ago.

永远不要使用它.

在古老的 C 语言中,在您出生前十年,您可以声明一个没有参数的函数.当您调用它时,编译器会将参数提升为默认类型并将它们传递给函数.本质上,编译器从调用中的参数中找出了参数声明.

In ancient C, a decade before you were born, you could declare a function with no parameters. When you called it, the compiler would promote the parameters to default types and pass them to the function. Essentially, the compiler figured out the parameter declarations from the arguments in the call.

该函数仍应定义带参数,并且它们应与调用函数的方式相匹配.

The function should still be defined with parameters, and they should match the way the function is called.

1 功能"这个词用错了.这是当时做事的方式,因为更好的方法尚未广泛开发.在一种新的编程语言中,这种特性会被认为是一种缺陷.

1 "Feature" is the wrong word. It was the way things were done at the time, since better ways were not yet widely developed. In a new programming language, this characteristic would be considered a deficiency.

这篇关于空参数列表是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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