开发者

C variable declarations after function heading in definition [duplicate]

开发者 https://www.devze.com 2022-12-27 07:11 出处:网络
This question already has answers here: What is this strange function definition syntax in C? [duplicate]
This question already has answers here: What is this strange function definition syntax in C? [duplicate] (6 answers) Closed 8 years ago.

When reading some FreeBSD sour开发者_如何学JAVAce 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) {


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


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.


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`
0

精彩评论

暂无评论...
验证码 换一张
取 消