编译此代码时出现意外结果 [英] Unexpected Result while compiling this code

查看:93
本文介绍了编译此代码时出现意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


请参阅以下代码。


int main()

{

double da = 0.01,db;

db = foo(da);

}

double foo(double a )

{

返回a;

}


当我使用gcc编译此代码时'得到类型不匹配的警告

但不是编译错误。


请帮我找出发生了什么。


先谢谢。

Hello,

Please see the following code.

int main ()
{
double da = 0.01, db;
db = foo (da);
}
double foo (double a)
{
return a;
}

when i compile this code using gcc i''m getting type-mismatch warnings
but not compilation error.

Please help me in finding out what is going on.

Thanks in advance.

推荐答案

Sunil Varma< su ******* @ gmail。 COM>写道:
Sunil Varma <su*******@gmail.com> wrote:

int main()
{da /> double da = 0.01,db;
db = foo(da);
}
双foo(双a)
{
返回;
}
当我用gcc编译这段代码时我得到的类型不匹配警告
但不是编译错误。

int main ()
{
double da = 0.01, db;
db = foo (da);
}
double foo (double a)
{
return a;
}

when i compile this code using gcc i''m getting type-mismatch warnings
but not compilation error.




您的代码在声明之前调用foo()。移动上面的函数

main(),或者在main之前声明一个原型:


double foo(double a);


如果在编译器知道其定义之前调用函数,则它将假定函数返回一个int
。稍后,当它第一次实际看到

定义时,它会告诉你类型不匹配,

因为初始假设(foo()返回int )是不一致的

与实际定义(foo()返回双倍)


-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C



Your code calls foo() before it is declared. Move the function above
main(), or declare a proto before main :

double foo (double a);

If you call a function before the compiler knows its definition, it will
assume the function returns an int. Later on, when it actually sees the
definition for the first time, it will tell you about the type-mismatch,
because the initial assumption (foo() returning int) is inconsistent
with the actual definition (foo() returning double)

--
:wq
^X^Cy^K^X^C^C^C^C


Sunil Varma写道:
Sunil Varma wrote:
您好,

请参阅以下代码。


double foo(double);

int main()
{
double da = 0.01,db;
db = foo(da);
}
双foo(双a)
{
返回;
}
我编译的时候这段代码使用gcc我得到类型不匹配
警告而不是编译错误。

请帮我找出发生了什么。

谢谢提前。
Hello,

Please see the following code.
double foo(double);
int main ()
{
double da = 0.01, db;
db = foo (da);
}
double foo (double a)
{
return a;
}

when i compile this code using gcc i''m getting type-mismatch
warnings but not compilation error.

Please help me in finding out what is going on.

Thanks in advance.




如果没有上面添加的行(

foo的前向声明),编译器不知道foo()第一次它b $ b看到它,并且,正如标准所要求的那样,假设它返回一个

int,因此你的警告。


干杯


弗拉基米尔



Without the line I have added above (forward declaration of
foo), the compiler does not know about foo() the first time it
sees it, and, as the standard requires, assumes it returns an
int, hence your warning.

Cheers

Vladimir


您看到了哪些警告?当我在这里尝试编译时(gcc

4.0.2)它_did_给我一个错误,引用了函数foo,因为

在这里重复:

Which warnings are you seeing? When I tried to compile that here (gcc
4.0.2) it _did_ give me an error with a reference to function foo, as
replicated here:


这篇关于编译此代码时出现意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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