后面返回0在C / C默认值++的理由 [英] Rationale behind return 0 as default value in C/C++

查看:87
本文介绍了后面返回0在C / C默认值++的理由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有为什么零作为默认函数的返回值的一个原因?我注意到,从STDLIB多种功能,几乎其他地方一样,当没有返回正确的号码(例如战俘(),strcpy的())或错误(负数),只需返回零。

Is there a reason why zero is used as a "default" function return value? I noticed that several functions from the stdlib and almost everywhere else, when not returning a proper number (e.g pow(), strcpy()) or an error (negative numbers), simply return zero.

我只是看到与否定的逻辑进行的多次测试后,成为了好奇。非常混乱。

I just became curious after seeing several tests performed with negated logic. Very confusing.

为什么不返回1,或为0xFF,或任何为此事正数?

Why not return 1, or 0xff, or any positive number for that matter?

推荐答案

的理由是要区分一组对应不同的错误,所有可能的(负)返回值的唯一情况,即都去确定。追求这种区分的最简单,最简洁,最C-ISH方法是逻辑测试,并且由于在C中的所有整数为真除了零,你想回到零的意思是唯一的局面,即你想零作为好的价值。

The rationale is that you want to distinguish the set of all the possible (negative) return values corresponding to different errors from the only situation in which all went OK. The simplest, most concise and most C-ish way to pursue such distinction is a logical test, and since in C all integers are "true" except for zero, you want to return zero to mean "the only situation", i.e. you want zero as the "good" value.

推理的同一线路适用于Unix程序的返回值,但确实是在UNIX shell脚本中的测试逻辑反转:0的返回值意味着真(比如,看的返回值/斌/真)。

The same line of reasoning applies to the return values of Unix programs, but indeed in the tests within Unix shell scripts the logic is inverted: a return value of 0 means "true" (for example, look at the return value of /bin/true).

这篇关于后面返回0在C / C默认值++的理由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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