C variable declarations after function heading in definition [duplicate]

流过昼夜 提交于 2019-11-26 11:39:23

问题


When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations that followed the \"function heading\" in the definition.

Is this valid in ISO C (C99)? when should this be done in production code instead of just declaring the variables within the \"function heading?\" Why is it being done here?

I refer to the function heading the string that looks like this: int someFunction(int i, int b) {


回答1:


That looks like K&R (pre-ANSI) style. I don't think it's valid C99, but are they using C99? Joel




回答2:


I think you are referring to the "old-fashioned" pre-ANSI way of declaring parameters in C. It looked something like this:

int foo(a, b)
    int a,
    int b
{
    /* ... */
}

That may still be valid in C99, and will be accepted by compilers for backward-compatibility reasons, but it should be considered deprecated/archaic.




回答3:


Er. Maybe I'm misunderstanding your question, but i and b in that snippet are parameters to the function. It's not some compact way of declaring variables within the function, like:

int someFunction() {
    int i, b;

When you call someFunction, you pass it those arguments:

someFunction(1, 2); // `i` will be `1` and `b` `2` within `someFunction`


来源:https://stackoverflow.com/questions/2633776/c-variable-declarations-after-function-heading-in-definition

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