K&R style function definition problem

不想你离开。 提交于 2019-11-29 03:38:43

Actually, kind of an interesting question.

This has to do with the evolution of the C language and the way it arranges to be backwards-compatible to the older flavors.

In both cases, you have a K&R-era definition for foo(), but a C99 declaration (with prototype) earlier.

But in the first case, the default parameter of int actually is the parameter, so the function call is compatible.

In the second case, though, the K&R definition brings in the standard argument promotions rule from the K&R era and the type of the parameter is really double.

But, you used a modern prototype at the call site, making it a float. So the code at the call site might have pushed a real float, which is in any case a distinct type from double.

If all of the references to foo() were K&R style, I believe the most you would get would be a warning, which is what compilers would have done back then, and the compiler must act like that to compile the legacy code. It would even have been a type-safe call because the floats would all be promoted to double, at least for the procedure call interface. (Not necessarily for the internal function code.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!