C function syntax, parameter types declared after parameter list

前端 未结 7 606
一个人的身影
一个人的身影 2020-11-22 12:34

I\'m relatively new to C. I\'ve come across a form of function syntax I\'ve never seen before, where the parameter types are defined after that parameter list. Can someone e

相关标签:
7条回答
  • 2020-11-22 13:07

    While the old syntax for function definition still works (with warnings, if you ask your compiler), using them does not provide function prototypes.
    Without function prototypes the compiler will not check if the functions are called correctly.

    #include <stdio.h>
    int foo(c)
    int c;
    { return printf("%d\n", c); }
    
    int bar(x)
    double x;
    { return printf("%f\n", x); }
    
    int main(void)
    {
        foo(42); /* ok */
        bar(42); /* oops ... 42 here is an `int`, but `bar()` "expects" a `double` */
        return 0;
    }
    

    When the program is run, the output on my machine is

    $ gcc proto.c
    $ gcc -Wstrict-prototypes proto.c
    proto.c:4: warning: function declaration isn’t a prototype
    proto.c:10: warning: function declaration isn’t a prototype
    $ ./a.out
    42
    0.000000
    
    0 讨论(0)
提交回复
热议问题