Why does a function with no parameters (compared t

2019-01-01 03:12发布

I've just come across someone's C code that I'm confused as to why it is compiling. There are two points I don't understand.

First, the function prototype has no parameters compared to the actual function definition. Second, the parameter in the function definition does not have a type.

#include <stdio.h>

int func();

int func(param)
{
    return param;
}

int main()
{
    int bla = func(10);    
    printf("%d", bla);
}

Why does this work? I have tested it in a couple of compilers, and it works fine.

10条回答
步步皆殇っ
2楼-- · 2019-01-01 03:17

Regarding parameter type, there are already correct answers here but if you want to hear it from the compiler you can try adding some flags (flags are almost always a good idea anyways).

compiling your program using gcc foo.c -Wextra I get:

foo.c: In function ‘func’:
foo.c:5:5: warning: type of ‘param’ defaults to ‘int’ [-Wmissing-parameter-type]

strangely -Wextra doesn't catch this for clang (it doesn't recognize -Wmissing-parameter-type for some reason, maybe for historical ones mentioned above) but -pedantic does:

foo.c:5:10: warning: parameter 'param' was not declared, 
defaulting to type 'int' [-pedantic]
int func(param)
         ^
1 warning generated.

And for prototype issue as said again above int func() refers to arbitrary parameters unless you exclicitly define it as int func(void) which would then give you the errors as expected:

foo.c: In function ‘func’:
foo.c:6:1: error: number of arguments doesn’t match prototype
foo.c:3:5: error: prototype declaration
foo.c: In function ‘main’:
foo.c:12:5: error: too many arguments to function ‘func’
foo.c:5:5: note: declared here

or in clang as:

foo.c:5:5: error: conflicting types for 'func'
int func(param)
    ^
foo.c:3:5: note: previous declaration is here
int func(void);
    ^
foo.c:12:20: error: too many arguments to function call, expected 0, have 1
    int bla = func(10);
              ~~~~ ^~
foo.c:3:1: note: 'func' declared here
int func(void);
^
2 errors generated.
查看更多
萌妹纸的霸气范
3楼-- · 2019-01-01 03:18

int func(); is an obsolescent function declaration from the days when there was no C standard, i.e. the days of K&R C (before 1989, the year the first "ANSI C" standard was published).

Remember that there were no prototypes in K&R C and the keyword void was not yet invented. All you could do was to tell the compiler about the return type of a function. The empty parameter list in K&R C means "an unspecified but fixed" number of arguments. Fixed means that you must call the function with the same number of args each time (as opposed to a variadic function like printf, where the number and type can vary for each call).

Many compilers will diagnose this construct; in particular gcc -Wstrict-prototypes will tell you "function declaration isn't a prototype", which is spot on, because it looks like a prototype (especially if you are poisoned by C++!), but isn't. It's an old style K&R C return type declaration.

Rule of thumb: Never leave an empty parameter list declaration empty, use int func(void) to be specific. This turns the K&R return type declaration into a proper C89 prototype. Compilers are happy, developers are happy, static checkers are happy. Those mislead by^W^Wfond of C++ may cringe, though, because they need to type extra characters when they try to exercise their foreign language skills :-)

查看更多
闭嘴吧你
4楼-- · 2019-01-01 03:20

This is why I typically advise people to compile their code with:

cc -Wmissing-variable-declarations -Wstrict-variable-declarations -Wold-style-definition

These flags enforce a couple of things:

  • -Wmissing-variable-declarations: It is impossible to declare a non-static function without getting a prototype first. This makes it more likely that a prototype in a header file matches with the actual definition. Alternatively, it enforces that you add the static keyword to functions that don't need to be visible publicly.
  • -Wstrict-variable-declarations: The prototype must properly list the arguments.
  • -Wold-style-definition: The function definition itself must also properly list the arguments.

These flags are also used by default in a lot of Open Source projects. For example, FreeBSD has these flags enabled when building with WARNS=6 in your Makefile.

查看更多
查无此人
5楼-- · 2019-01-01 03:25

All the other answers are correct, but just for completion

A function is declared in the following manner:

  return-type function-name(parameter-list,...) { body... }

return-type is the variable type that the function returns. This can not be an array type or a function type. If not given, then int is assumed.

function-name is the name of the function.

parameter-list is the list of parameters that the function takes separated by commas. If no parameters are given, then the function does not take any and should be defined with an empty set of parenthesis or with the keyword void. If no variable type is in front of a variable in the paramater list, then int is assumed. Arrays and functions are not passed to functions, but are automatically converted to pointers. If the list is terminated with an ellipsis (,...), then there is no set number of parameters. Note: the header stdarg.h can be used to access arguments when using an ellipsis.

And again for the sake of completeness. From C11 specification 6:11:6 (page: 179)

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

查看更多
姐姐魅力值爆表
6楼-- · 2019-01-01 03:26

In C func() means that you can pass any number of arguments. If you want no arguments then you have to declare as func(void). The type you're passing to your function, if not specified defaults to int.

查看更多
看风景的人
7楼-- · 2019-01-01 03:27

As stated @Krishnabhadra, all previous responses from other users, have a correct interpretation, and I just want to make a more detailed analysis of some points.

In the Old-C as in ANSI-C the "untyped formal parameter", take the dimencion of your work register or instruction depth capability (shadow registers or instruction cumulative cycle), in an 8bit MPU, will be an int16, in a 16bit MPU and so will be an int16 an so on, in the case 64bit architectures may choose to compile options like: -m32.

Although it seems simpler implementation at high level, For pass multiple parameters, the work of the programmer in the control dimencion data type step, becomes more demanding.

In other cases, for some microprocessors architectures, the ANSI compilers customized, leveraged some of this old features to optimize the use of the code, forcing the location of these "untyped formal parameters" to work within or outside the work register, today you get almost the same with the use of "volatile" and "register".

But it should be noted that the most modern compilers, not make any distinction between the two types of parameters declaration.

Examples of a compilation with gcc under linux:

main.c

main2.c

main3.c  
In any case the statement of the prototype locally is of no use, because there is no call without parameters reference to this prototype will be remiss. If you use the system with "untyped formal parameter", for an external call, proceed to generate a declarative prototype data type.

Like this:

int myfunc(int param);
查看更多
登录 后发表回答