C function with no parameters behavior

前端 未结 2 1944
时光说笑
时光说笑 2020-11-29 05:46

Can somebody explain to me why the following code does compile without a warning or error?

I would expect the compiler to warn me that the function no_args

相关标签:
2条回答
  • 2020-11-29 06:17

    When you declare a function with an empty argument list, you invoke K&R (pre-prototype) semantics and nothing is assumed about the parameter list; this is so that pre-ANSI C code will still compile. If you want a prototyped function with an empty parameter list, use (void) instead of ().

    0 讨论(0)
  • 2020-11-29 06:26

    In C++, void no_args() declares a function that takes no parameters (and returns nothing).

    In C, void no_args() declares a function that takes an unspecified (but not variable) number of parameters (and returns nothing). So all your calls are valid (according to the prototype) in C.

    In C, use void no_args(void) to declare a function that truly takes no parameters (and returns nothing).

    0 讨论(0)
提交回复
热议问题