Alternative (K&R) C syntax for function declaration versus prototypes

后端 未结 5 679
余生分开走
余生分开走 2020-11-21 22:30

What is useful about this C syntax — using \'K&R\' style function declarations?

int func (p, p2)
    void* p;
    int  p2;
{
    return 0;
}         


        
5条回答
  •  深忆病人
    2020-11-21 23:11

    That's a relic from when C had no prototypes for functions. Way back then, (I think) functions were assumed to return int and all its arguments were assumed to be int. There was no checking done on function parameters.

    You're much better off using function prototypes in the current C language.
    And you must use them in C99 (C89 still accepts the old syntax).

    And C99 requires functions to be declared (possibly without a prototype). If you're writing a new function from scratch, you need to provide a declaration ... make it a prototype too: you lose nothing and gain extra checking from the compiler.

提交回复
热议问题