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:27

If the function declaration has no parameters i.e. empty then it is taking unspecified number of arguments. If you want to make it take no arguments then change it to:

int func(void);
查看更多
怪性笑人.
3楼-- · 2019-01-01 03:32

It's K&R style function declaration and definition. From C99 Standard (ISO/IEC 9899:TC3)

Section 6.7.5.3 Function Declarators (including prototypes)

An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied. (If both function types are "old style", parameter types are not compared.)

Section 6.11.6 Function declarators

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

Section 6.11.7 Function definitions

The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.

Which the old style means K&R style

Example:

Declaration: int old_style();

Definition:

int old_style(a, b)
    int a; 
    int b;
{
     /* something to do */
}
查看更多
素衣白纱
4楼-- · 2019-01-01 03:35
  • The empty parameter list means "any arguments", so the definition isn't wrong.
  • The missing type is assumed to be int.

I would consider any build that passes this to be lacking in configured warning/error level though, there's no point in being this allowing for actual code.

查看更多
何处买醉
5楼-- · 2019-01-01 03:43

C assumes int if no type is given on function return type and parameter list. Only for this rule following weird things are possible.

A function definition looks like this.

int func(int param) { /* body */}

If its a prototype you write

int func(int param);

In prototype you can only specify the type of parameters. Parameters' name is not mandatory. So

int func(int);

Also if you dont specify parameter type but name int is assumed as type.

int func(param);

If you go farther, following works too.

func();

Compiler assumes int func() when you write func(). But dont put func() inside a function body. That'll be a function call

查看更多
登录 后发表回答