为什么与INT参数的定义,而不是浮动参数的空申报工作? [英] Why does an empty declaration work for definitions with int arguments but not for float arguments?

查看:134
本文介绍了为什么与INT参数的定义,而不是浮动参数的空申报工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得不同的是,声明没有参数类型...

为什么这项工作:

  INT FUC();INT FUC(int i)以{
  的printf(%D,我);
  返回0;
}

但这种失败编译:

  INT FUC();INT FUC(浮点六){
  的printf(%F,F);
  返回0;
}

与消息:


  

错误:冲突的类型FUC。注意:有一个默认的促销参数类型不能匹配空参数名列表声明



解决方案

一个声明:

  INT F();

...告诉一些标识(˚F,在这种情况下)名称的功能,并告诉它的函数的返回类型的编译器 - 但确实的指定号码或功能用于接收参数(S)的类型(S)。

一个原型:

  INT F(INT,CHAR);

...是那些类似,但是还指定数目/类型的功能是用来接收参数(多个)。如果不带参数,可以使用类似 INT F(无效)来指定(因为让括号空是一个声明)。一种新的风格函数定义:

  INT F(int类型的,字符B){
    //做的东西在这里...
}

...也用作原型

如果没有范围的原型,编译器在调用之前应用默认促销参数。这意味着任何字符它提升到 INT ,任何浮动提升为双击。因此,如果声明(而不是原型)的函数,你做的的要指定任何字符浮动参数 - 呼吁这样的事情/会给未定义的行为。在默认的标志,编译器可能拒绝code,因为基本上没有办法正确地使用它。你也许可以找到一些一套编译器标志的,将得到它接受code,但它会是pretty毫无意义的,因为你不能无论如何使用它...

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

解决方案

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...

这篇关于为什么与INT参数的定义,而不是浮动参数的空申报工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆