为什么在括号外声明参数类型?

Vij*_*jay 10 c coding-style function

有时我看到函数定义如下:

read_dir(dir)
char    *dir;   
{
        DIR * dirp;
        struct dirent *d;

        /* open directory */
        dirp = opendir(dir);
  ......... so  on
Run Code Online (Sandbox Code Playgroud)

这里声明的重要性是什么

char    *dir;
Run Code Online (Sandbox Code Playgroud)

在函数名称之后很快声明指针然后启动函数体的内涵是什么.

unw*_*ind 12

这是一种较旧的C语法,它取代了" K&R C ",因为它出现在传奇书籍的原始版本中.

以前写的是这样的:

foo(a, b)
int a;
int b;
{
}
Run Code Online (Sandbox Code Playgroud)

就是现在

int foo(int a, int b)
{
}
Run Code Online (Sandbox Code Playgroud)


vla*_*adr 8

它只是"旧式",K&R C功能定义(参见Kernighan&Ritchie的书,通常简称为Kernighan&Ritchie.)

您提到的代码可能是在八十年代末期或九十年代早期编写的,具有可移植性(即与较旧编译器的兼容性,可能在更多"异国情调"平台上).

即使在1989 C标准出版之后,多年来K&R C仍然被认为是C程序员在需要最大可移植性时限制自己的"最低共同点",因为许多较旧的编译器仍然在使用,并且因为经过精心编写的K&R C代码也可以是合法的标准C.

有些人可能认为仍然由编译器支持的K&R风格的函数定义更具可读性,实际上并不一定是真的; 相比:

some_function(param1,param2,param3)
char    *param1;   /* param1 comment */
int     param2;    /* param2 comment */
short   param3;    /* param3 comment */
{
}
Run Code Online (Sandbox Code Playgroud)

/* notice also that return type is explicitly specified now */
int
some_function(
  char    *param1, /* param1 comment */
  int     param2,  /* param2 comment */
  short   param3   /* param3 comment */
)   
{
}
Run Code Online (Sandbox Code Playgroud)

自1989年以来, K&R风格的功能定义已经过时 ; 请参阅C90标准中的第6.9.5节"功能定义".