Declaring function parameters after function name

后端 未结 5 384
伪装坚强ぢ
伪装坚强ぢ 2020-12-11 19:38
int func(x)
int x;
{
    .............

What is this kind of declaration called?

When is it valid/invalid including C or C++, certain standa

相关标签:
5条回答
  • 2020-12-11 19:58

    It's still valid, but it's pre-ANSI. That's actually where the K&R indent style got its name. The opening bracket is on the line after the function block because this looks weird:

    int func(x)
    int x; {
    ...
    }
    

    Anyway, this style is not recommended because of a problem with function prototypes.

    0 讨论(0)
  • 2020-12-11 20:00

    That is K&R C parameter declaration syntax, which is valid in ANSI C but not in C++.

    0 讨论(0)
  • 2020-12-11 20:15

    It's a function prototype. If you didn't do it this way you'd have to write the function out entirely before main, otherwise the compiler wouldn't know what the function was when you used it in main. It's not very descriptive, so it's not used anymore. You'd want to use something like:

    int someFunction(int someParamX int someParamY);
    
    0 讨论(0)
  • 2020-12-11 20:16

    That's old-style C. It's seldom seen anymore.

    0 讨论(0)
  • 2020-12-11 20:21

    K&R style, and I think it's still valid, although discouraged. It probably came from Fortran (where function parameters types are defined inside the function body still in the recent F95)

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