Why is void f(…) not allowed in C?

后端 未结 6 1712
悲&欢浪女
悲&欢浪女 2021-01-05 15:31

Why doesn\'t C allow a function with variable length argument list such as:

void f(...)
{
    // do something...
}
6条回答
  •  醉话见心
    2021-01-05 15:51

    There's no an intrisic reason why C can't accept void f(...). It could, but "designers" of this C feature decided not to do so.

    My speculation about their motivations is that allowing void f(...) would require more "hidden" code (that can be accounted as a runtime) than not allowing it: in order to make distinguishable the case f() from f(arg) (and the others), C should provide a way to count how many args are given, and this needs more generated code (and likely a new keyword or a special variable like say "nargs" to retrieve the count), and C usually tries to be as minimalist as possible.

提交回复
热议问题