Declaring function parameters after function name

限于喜欢 提交于 2019-11-27 07:52:35

问题


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

What is this kind of declaration called?

When is it valid/invalid including C or C++, certain standard revisions and compilers?


回答1:


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.




回答2:


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




回答3:


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)




回答4:


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




回答5:


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);


来源:https://stackoverflow.com/questions/1151402/declaring-function-parameters-after-function-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!