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

后端 未结 5 553
余生分开走
余生分开走 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条回答
  •  猫巷女王i
    2020-11-21 23:07

    This is pretty old K&R C syntax (pre-dates ANSI/ISO C). Nowadays, you should not use it anymore (as you have already noticed its major disadvantage: the compiler won't check the types of arguments for you). The argument type actually defaults to int in your example.

    At the time, this syntax was used, one sometimes would find functions like

    foo(p, q) 
    {
        return q + p;
    }
    

    which was actually a valid definition, as the types for foo, p, and q default to int.

提交回复
热议问题