Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

c - Why does an empty declaration work for definitions with int arguments but not for float arguments?

I thought the difference is that declaration doesn't have parameter types...

Why does this work:

int fuc();

int fuc(int i) {
  printf("%d", i);
  return 0;
}

but this fails compiling:

int fuc();

int fuc(float f) {
  printf("%f", f);
  return 0;
}

with the message:

error: conflicting types for ‘fuc’. note: an argument type that has a default promotion can’t match an empty parameter name list declaration

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A declaration:

int f();

...tells the compiler that some identifier (f, in this case) names a function, and tells it the return type of the function -- but does not specify the number or type(s) of parameter(s) that function is intended to receive.

A prototype:

int f(int, char);

...is otherwise similar, but also specifies the number/type of parameter(s) the function is intended to receive. If it takes no parameter, you use something like int f(void) to specify that (since leaving the parentheses empty is a declaration). A new-style function definition:

int f(int a, char b) { 
    // do stuff here...
}

...also acts as a prototype.

Without a prototype in scope, the compiler applies default promotions to arguments before calling the function. This means that any char or short it promoted to int, and any float is promoted to double. Therefore, if you declare (rather than prototype) a function, you do not want to specify any char, short or float parameter -- calling such a thing would/will give undefined behavior. With default flags, the compiler may well reject the code, since there's basically no way to use it correctly. You might be able to find some set of compiler flags that would get it to accept the code but it would be pretty pointless, since you can't use it anyway...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...