int foo (int argc, …) vs int foo() vs int foo(void) in C

前端 未结 4 1161
不思量自难忘°
不思量自难忘° 2021-01-13 02:17

So today I figured (for the first time admittedly) that int foo() is in fact different from int foo(void) in that the first one allows any

4条回答
  •  野的像风
    2021-01-13 02:41

    First, take int foo(). This form is a function declaration that does not provide a prototype - that is, it doesn't specify the number and types of its arguments. It is an archaic form of function declaration - it's how functions were originally declared in pre-ANSI C. Such a function however does have a fixed number and type of arguments - it's just that the declaration doesn't tell the compiler what it is, so you are responsible for getting it right where you call the function. You declare such a function with arguments like so:

    int foo(a, b)
        int a;
        char *b;
    {
        return a + strlen(b);
    }
    

    This form of function declaration is now obsolete.

    int foo(void) is a modern declaration that provides a prototype; it declares a function that takes no arguments. int foo(int argc, ...) also provides a prototype; it declares a function that has one fixed integer argument and a variable argument list.

提交回复
热议问题