函数的返回值 [英] return value of functions

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

问题描述

大家好!

我是C的初学者。当我学习C时,我发现了很多C教学书中描述的问题并不是很好的b $ b 。就像这样:我发现一些int

或char *函数可以用作void函数。例如:我们可以使用

int函数printf()之类的这:

printf(" hello,world!\ n");

它是一个无效函数!

我们也是可以像这样使用char * function ltoa():

ltoa(ltemp,buffer,10);

为什么?

Hi,everyone!
I am a beginner of C.When I studied C,I found a problem which isn''t
described in many C teaching books.It is like this: I found some of int
or char* functions can be used as void function.For example: we can use
int function printf() like this :
printf("hello,world!\n");
How it is like a void function!
Also we can use char* function ltoa()like this:
ltoa(ltemp,buffer,10);
Why??

推荐答案



yohji写道:

yohji wrote:
大家好!
我是C.的初学者。当我研究过C,我发现了许多C教学书中没有描述的问题。就像这样:我发现一些
int或char *函数可以用作void函数。例如:我们可以
使用int函数printf()这样:
printf(" hello,world!\ n");
它是如何像一个虚空f我们也可以像这样使用char * function ltoa():
ltoa(ltemp,buffer,10);
为什么??
Hi,everyone!
I am a beginner of C.When I studied C,I found a problem which isn''t
described in many C teaching books.It is like this: I found some of int or char* functions can be used as void function.For example: we can use int function printf() like this :
printf("hello,world!\n");
How it is like a void function!
Also we can use char* function ltoa()like this:
ltoa(ltemp,buffer,10);
Why??



因为,你想要。有些语言可以_prohibited_

来丢弃函数的返回值。 C不是其中之一。许多程序员也倾向于写上面的函数。 as


(void)printf(" Hello World!\ n");

(无效)scanf("%d",&价值);


表示_they_知道他们正在丢弃价值。

BTW,什么是功能?上面做的事情是什么?


-

Imanpreet Singh Arora



Because, you want to. There are languages in which it is _prohibited_
to discard the return value of a function. C isn''t one of them. Also a
lot of programmers, do tend to write the above "functions" as

(void) printf("Hello World!\n");
(void) scanf("%d", &value);

To indicate that _they_ know that they are discarding the value.
BTW, What is the "function" thing doing above?

--
Imanpreet Singh Arora


yohji写道在09/04/05:
yohji wrote on 09/04/05 :
大家好!
我是C的初学者。当我学习C时,我发现了一个没有描述的问题许多C教学书籍。就像这样:我发现一些int
或char *函数可以用作void函数。


让我们的改写更准确......


C函数汽车返回somethinf或no。当函数返回

时,它的返回类型为''void''。在其他情况下,有一个

返回类型(''int'',''char *'',或者其他......)。


但是当你使用非空函数时,你不需要/使用它的

返回值(但在很多情况下,你应该,因为值已经

可能是一些重要的意义)。


相反,从函数返回任何内容获取值

调用未定义的行为(IOW,一个bug )

例如:我们可以像这样使用
int函数printf():
printf(" hello,world!\ n");
如何它就像一个void函数!
我们也可以像这样使用char * function ltoa():
ltoa(ltemp,buffer,10);
为什么??
Hi,everyone!
I am a beginner of C.When I studied C,I found a problem which isn''t
described in many C teaching books.It is like this: I found some of int
or char* functions can be used as void function.
Let''s rephrase is more accurately...

C functions car return somethinf or no. When a function returns
nothing, its return type is ''void''. On the other cases, there is a
return type (''int'', ''char *'', or whatever...).

But when you use a non-void function, you don''t /have to/ use its
returned value (but in many cases, you should, because the value has
probably some important meaning).

On the contrary, getting a value from a function returning nothing
invokes an undefined behaviour (IOW, a bug)
For example: we can use
int function printf() like this :
printf("hello,world!\n");
How it is like a void function!
Also we can use char* function ltoa()like this:
ltoa(ltemp,buffer,10);
Why??




这取决于你的偏执级别...


请注意,ltoa()不是标准的C函数。

-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html


显然你的代码不符合原始规格。

你用湿面条判处30睫毛。

- Jerry Coffin in alcc ++



It depends on your paranoid level...

Note that ltoa() is not a standard C function.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++


yohji写道:
大家好!
我是C.的初学者。当我学习C时,我发现在许多C教学书籍中都没有描述过这个问题。就像这样:我发现一些int
或char *函数可以用作void函数。例如:我们可以用
int function printf()是这样的:
printf(" hello,world!\ n");
它是如何像一个void函数!


您可以随时丢弃表达式的值。这就是

在这里完成的。你打电话给printf是因为它的副作用,而不是它的价值。类似地,表达式''x ++''具有值和

副作用,可以在语句中使用

x ++;

丢弃该值,仅关注副作用(递增x)。

注意''a = b''也是一个产生值的表达式,但你会

不要觉得特别奇怪看到

a = b;

丢弃表达式的值并仅使用
$ b $的副作用b将b的值赋给a。

我们也可以像这样使用char * function ltoa():
ltoa(ltemp,buffer,10);
Hi,everyone!
I am a beginner of C.When I studied C,I found a problem which isn''t
described in many C teaching books.It is like this: I found some of int
or char* functions can be used as void function.For example: we can use
int function printf() like this :
printf("hello,world!\n");
How it is like a void function!
You can always discard the value of an expression. That is all that is
being done here. You are calling printf for its side-effect, not for
its value. Similarly, the expression ''x++'' has both a value and a
side-effect and can be used in the statement
x++;
discarding the value, caring only about the side-effect (incrementing x).
Note that ''a = b'' is also an expression yielding a value, but you would
not consider it particularly strange to see
a = b;
discarding the value of the expression and using only the side-effect of
assigning the value of b to a.
Also we can use char* function ltoa()like this:
ltoa(ltemp,buffer,10);




没有名为''itoa'的标准函数,任何语法或语义

都是实现定义的。如果一个函数返回一个char *,它可以是

a中的任何一个指向(或进入)参数列表指定的缓冲区的指针,

b)指向静态缓冲区的指针

c)指向动态分配缓冲区的指针。

d)错误指示(例如NULL)

如果情况(c)成立,那么丢弃返回值将导致

内存泄漏,你会觉得非常愚蠢。



There is no standard function named ''itoa'' and any syntax or semantics
are implementation-defined. If a function returns a char *, that could
be any of
a) a pointer to (or into) a buffer specified by the argument list,
b) a pointer to a static buffer
c) a pointer to a dynamically allocated buffer.
d) an error indication (e.g. NULL)
If case (c) holds, then discarding the return value will lead to a
memory leak, and you will feel very silly.


这篇关于函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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