使用"无效"用C [英] Using "void" in C

查看:117
本文介绍了使用"无效"用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑,为什么我们需要传递无效进入C函数:

I'm confused about why we need to pass void into C functions:

int f(void) { return 0; }

int f() { return 0; }

什么是正确的事情,为什么?

What is the proper thing to do and why?

推荐答案

在C, INT F()是一个旧式的声明。它说,˚F需要一个固定的,但数目不详和参数类型(S)。例如,给定 INT F(){返回0; } ,编译器不会抱怨 F(42) F(你好,世界)(尽管这种调用的行为是不确定的)。

In C, int f() is an old-style declaration. It says that f expects a fixed but unspecified number and type(s) of arguments. For example, given int f() { return 0; }, the compiler won't complain about f(42), or f("hello, world") (though the behavior of such a call is undefined).

INT F(无效)明确地说,˚F不带任何参数。

int f(void) explicitly says that f takes no arguments.

(C ++有不同的规则。斯特劳斯并不像关心与旧的C code向后兼容)

(C++ has different rules; Stroustrup wasn't as concerned about backward compatibility with old C code.)

这篇关于使用"无效"用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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