我想使用不返回int的函数,而不声明它们 [英] i want to use functions that don't return int, without declaring them

查看:130
本文介绍了我想使用不返回int的函数,而不声明它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果我想调用不返回int的函数而不声明

,那会有什么危害吗? />

我只想将函数(返回值)分配给

返回的类型,所以我不知道返回值如何发挥作用在这里。

Ex

int main

{

double val;


val = func();

/ *当返回值来到这里时,它是一个双* /


返回EXIT_SUCCESS;

}

double func(无效)

{

返回5;

/ *这将转换回传前5到双倍价值* /

}


我知道为什么我们有原型,但声明我不知道他们是怎么回事

很有用。如果我们(程序员)知道函数返回的类型是什么,那么实际函数的代码会将返回值

转换为正确的类型。那为什么我们需要声明?怎么

他们有帮助吗?

Hi,

If I want to call functions that don''t return int without declaring
them, will there be any harm?

I only want to assign the function(return value) to the type that it
returns, so I don''t see how the return value comes to play here.
Ex
int main
{
double val;

val = func();
/* when return value comes here, its a double */

return EXIT_SUCCESS;
}
double func(void)
{
return 5;
/* this will convert 5 to a double value before passing back*/
}

I know why we have prototypes, but declarations I have no clue how they
are useful. The actual function''s code will convert the return value
to the proper type, in the call location if we(programmer) know what
type the function is returning, then why do we need declarations? How
do they help?

推荐答案

2005-02-16 19:22:01 - 0500,G Patel < GA ******** @ gmail.com>说:
On 2005-02-16 19:22:01 -0500, "G Patel" <ga********@gmail.com> said:


如果我想调用不返回int的函数而不声明它们,会不会有任何伤害?

我只想将函数(返回值)分配给它返回的类型,所以我不知道返回值是如何在这里发挥作用的。


这是未定义的行为,不要这样做。它可能在一些

平台上偶然使用,但你无法保证。


想象一下,例如,一个具有单独浮点的平台

用于返回函数结果的寄存器和整数寄存器

Ex

int main
{
双val;


在这个假设的平台上,编译器说嘿,这个函数

func必须是''int func()'',所以我应该看一下对于

整数寄存器中的返回值"。

它调用函数,并取整数寄存器中的值

(这是可能垃圾)。

然后它将该垃圾int值转换为double,val现在有一个

垃圾值。

val = func( );
/ *当返回值来到这里时,它是一个双* /

返回EXIT_SUCCESS;
}

double func(void)
{
返回5;
/ *这会在传回之前将5转换为双倍值* /


是的,它会在我们的假设平台上它将值b / b
放入浮点寄存器,同时保留整数寄存器

的未定义值。 }



....或者这些都不会发生,这就是未定义

行为的本质。故事的道德:不要这样做。


另外,请注意你的代码甚至不能在C99编译器上编译,其中

隐式不再支持int。

我知道为什么我们有原型,但声明我不知道它们是如何有用的。如果我们(程序员)知道函数返回的类型,那么实际函数的代码会将返回值
转换为正确的类型,那么为什么我们需要声明呢?他们如何帮助?
Hi,

If I want to call functions that don''t return int without declaring
them, will there be any harm?

I only want to assign the function(return value) to the type that it
returns, so I don''t see how the return value comes to play here.
This is undefined behavior, don''t do it. It may work by chance on some
platforms, but you have no guarantee of that.

Imagine, for instance, a platform that has seperate floating point
registers and integer registers that are used to return function results
Ex

int main
{
double val;
On this hypothetical platform, the compiler says "hey, this function
func must be ''int func()'', so I should look for the return value in the
integer register".
It calls the function, and fetches the value in the integer register
(which is likely garbage).
It then converts that garbage int value to a double, val now has a
garbage value.
val = func();
/* when return value comes here, its a double */

return EXIT_SUCCESS;
}
double func(void)
{
return 5;
/* this will convert 5 to a double value before passing back*/
Yes it will, and on our hypothetical platform, it stuffs that value
into the floating point register, while leaving the integer register
with an undefined value. }

....Or none of that could happen, such is the nature of undefined
behavior. Moral of the story: don''t do it.

Also, note that your code won''t even compile on a C99 compiler where
implicit int is no longer supported.
I know why we have prototypes, but declarations I have no clue how they
are useful. The actual function''s code will convert the return value
to the proper type, in the call location if we(programmer) know what
type the function is returning, then why do we need declarations? How
do they help?




因为没有它们,你的程序会表现出未定义的行为。期间。


-

Clark S. Cox,III
cl ******* @ gmail.com



Because without them, your program exhibits undefined behavior. Period.

--
Clark S. Cox, III
cl*******@gmail.com




G Patel写道:

G Patel wrote:


如果我想调用不返回int的函数而不声明它们,


为什么?

会有什么危害吗?
Hi,

If I want to call functions that don''t return int without declaring
them,
Why?
will there be any harm?




这是非法的,你的编译器应该为

未声明的功能。只有返回int的函数才可以在旧标准下取消声明

。从C99开始,这甚至都不合法。


Brian



It''s illegal and your compiler should issue a diagnostic for an
undeclared function. Only functions returning int could be undeclared
under the old standard. As of C99, that''s not even legal.

Brian


G Patel写道:
G Patel wrote:

如果我想调用那些没有返回int而没有声明它们的函数,会不会有任何伤害?


是的。

我只想将函数(返回值)分配给它返回的类型
,所以我不是看看返回值是如何在这里玩的。

int main


不是一个很好的例子。

{
双重val;

val = func();
/ *当返回值来到这里时,它是一个双* /


不,它是一个整数,因为这就是你所隐含的,b $ b告诉编译器。请注意,您的代码是违反约束条件

在C99下。

返回EXIT_SUCCESS;
}

double func(void)
{
返回5;
/ *这会在传回之前将5转换为双值* /
}
我知道为什么我们有原型,


看来你不是。

但声明我不知道它们是如何有用的。
实际函数的代码如果我们(程序员)知道函数返回的类型,那么会将返回值
转换为正确的类型,在调用位置,那为什么我们需要
声明?它们如何帮助?

If I want to call functions that don''t return int without
declaring them, will there be any harm?
Yes.
I only want to assign the function(return value) to the type
that it returns, so I don''t see how the return value comes to
play here.

int main
Not a good example.
{
double val;

val = func();
/* when return value comes here, its a double */
No. It comes as an int, becasue that''s what you''ve implicitly
told the compiler. Note that your code is a constraint violation
under C99.
return EXIT_SUCCESS;
}

double func(void)
{
return 5;
/* this will convert 5 to a double value before passing back*/
}

I know why we have prototypes,
It seems that you don''t.
but declarations I have no clue how they are useful.
The actual function''s code will convert the return value
to the proper type, in the call location if we(programmer)
know what type the function is returning, then why do we need
declarations? How do they help?




实现通常具有返回

函数值的精确机制。这可以是堆栈或特定寄存器。

调用函数只知道

(可能隐含的)函数签名,就知道了返回机制。


更务实:func可以在堆栈上返回一个8字节的值,

或者它可以通过浮点寄存器返回一个值。鉴于

是一个隐式的int返回类型,调用函数可以假设

func在堆栈上返回一个4字节的int,或者通过
$ b返回一个值$ b整数寄存器。


-

彼得



Implementations typically have a precise mechanism for returning
function values. This may be a stack or specific register. The
calling function only knows the return mechanism by knowing
the (possibly implied) function signature.

More pragmatically: func may return an 8 byte value on a stack,
or it may return a value through a floating point register. Given
an implicit int return type, the calling function may assume that
func returns a 4 byte int on the stack, or a value through an
integer register.

--
Peter


这篇关于我想使用不返回int的函数,而不声明它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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