Empty parameter lists

C distinguishes between a function declared with an empty parameter list and a function declared with a parameter list consisting of onlyvoid. The former is an unprototyped function taking an unspecified number of arguments, while the latter is a prototyped function taking no arguments.

 // C code

    extern int  foo();          // Unspecified parameters
    extern int  bar(void);      // No parameters

    void baz()
    {
        foo(0);         // Valid C, invalid C++
        foo(1, 2);      // Valid C, invalid C++

        bar();          // Okay in both C and C++
        bar(1);         // Error in both C and C++
    } 

C++, on the other hand, makes no distinction between the two declarations and considers them both to mean a function taking no arguments.

    // C++ code

    extern int  xyz();

    extern int  xyz(void);  // Same as 'xyz()' in C++,
                            // Different and invalid in C 
For code that is intended to be compiled as either C or C++, the best solution to this problem is to always declare functions taking no parameters with an explicit void prototype. For example:

   // Compiles as both C and C++
    int bosho(void)
    {
        ...
    } 
Empty function prototypes are a deprecated feature in C99 (as they were in C89).


Refer to http://david.tribble.com/text/cdiffs.htm


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值