为什么声明放在func()和{}之间?

时间:2011-03-30 19:11:01

标签: c

在sed来源中,我经常看到

func(...)
int b;
char c;

{
...
}

为什么要把变量放在那里?它会改变范围吗?

像这里:http://www.mirrors.docunext.com/lxr/http/source/src/bin/sed/lib/regex_internal.c?v=haiku

re_string_allocate (pstr, str, len, init_len, trans, icase, dfa)
 51      re_string_t *pstr;
 52      const char *str;
 53      int len, init_len, icase;
 54      RE_TRANSLATE_TYPE trans;
 55      const re_dfa_t *dfa;
 56 {
 57   reg_errcode_t ret;
 58   int init_buf_len;
 59 

3 个答案:

答案 0 :(得分:3)

这是K& R(旧)风格,它有效,但是......

答案 1 :(得分:3)

这只是在C中声明参数的旧(K&R)方式。

/* Old way of declaring parameters*/
void myFunc(a, b)
  int a; int b;
{
    ...
}

除非你需要在真正的旧编译器上编译你的代码,否则没有人会这样做,所以要么sed是用旧编译器编写的,要么代码真的很旧。

答案 2 :(得分:2)

这只是旧样式,在ANSI C之前。具有类型参数的函数声明的整个概念直到稍后才被引入!