C语言 1 = 假和 0 = 真?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6475181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:00:04  来源:igfitidea点击:

1 = false and 0 = true?

clogic

提问by Ben

I came across an is_equals() function in a c API at work that returned 1 for non-equal sql tables (false) and 0 for equal ones (true). I only realized it after running test cases on my code, one for the positive example and one for the negative and they both failed which at first made little sense. The code in the API does not have a bug as the output was recorded correctly in its documentation.

我在工作中遇到 ac API 中的 is_equals() 函数,该函数返回 1 表示不相等的 sql 表(false)和 0 表示相等的表(true)。我只是在我的代码上运行测试用例后才意识到它,一个用于正面示例,一个用于负面示例,它们都失败了,这起初毫无意义。API 中的代码没有错误,因为输出已正确记录在其文档中。

My questions - are there upside down worlds / parallel universes / coding languages where this logical NOTing is normal? Isn't 1 usually true? Is the coder of the API making an error?

我的问题 - 是否存在颠倒的世界/平行宇宙/编码语言,其中这种逻辑 NOTing 是正常的?1 通常不是真的吗?API 的编码器是否出错?

回答by caf

It is common for comparison functions to return 0on "equals", so that they can also return a negative number for "less than" and a positive number for "greater than". strcmp()and memcmp()work like this.

比较函数通常0在“等于”时返回,因此它们也可以为“小于”返回负数,为“大于”返回正数。 strcmp()memcmp()像这样工作。

It is, however, idiomatic for zero to be false and nonzero to be true, because this is how the C flow control and logical boolean operators work. So it might be that the return values chosen for this function are fine, but it is the function's namethat is in error (it should really just be called compare()or similar).

然而,零为假而非零为真是惯用的,因为这就是 C 流控制和逻辑布尔运算符的工作方式。因此,为该函数选择的返回值可能很好,但错误的是函数的名称(它实际上应该只是被调用compare()或类似)。

回答by sarnold

This upside-down-world is common with process error returns. The shell variable $?reports the return value of the previous program to execute from the shell, so it is easy to tell if a program succeeds or fails:

这种颠倒的世界在过程错误返回中很常见。shell 变量$?报告从 shell 执行的前一个程序的返回值,因此很容易判断程序是成功还是失败:

$ false ; echo $?
1
$ true ; echo $?
0

This was chosen because there is a single case where a program succeeds but there could be dozens of reasons why a program fails -- by allowing there to be many different failure error codes, a program can determine whyanother program failed without having to parse output.

这被选中,是因为存在其中一个程序成功,但也可能有几十个为什么程序失败的原因一个案例-通过允许会有多个不同的故障错误代码,程序可以判断为什么另一个程序失败而不必解析输出.

A concrete example is the aa-statusprogram supplied with the AppArmormandatory access controltool:

一个具体的例子是AppArmor强制访问控制工具aa-status提供的程序:

   Upon exiting, aa-status will set its return value to the
   following values:

   0   if apparmor is enabled and policy is loaded.

   1   if apparmor is not enabled/loaded.

   2   if apparmor is enabled but no policy is loaded.

   3   if the apparmor control files aren't available under
       /sys/kernel/security/.

   4   if the user running the script doesn't have enough
       privileges to read the apparmor control files.

(I'm sure there are more widely-spread programs with this behavior, but I know this one well. :)

(我确信有更多广泛传播的程序具有这种行为,但我很了解这个。:)

回答by Peter K.

I suspect it's just following the Linux / Unix standardfor returning 0 on success.

我怀疑它只是遵循的Linux / Unix标准成功返回0

Does it reallysay "1" is false and "0" is true?

真的说“1”是假的,“0”是真的吗?

回答by Brennan Vincent

There's no good reason for 1to be true and 0to be false; that's just the way things have always been notated. So from a logical perspective, the function in your API isn't "wrong", per se.

1真实和0虚假没有充分的理由;这就是事物一直被标记的方式。因此,从逻辑的角度来看,您的 API 中的函数本身并没有“错误”。

That said, it's normally not advisable to work againstthe idioms of whatever language or framework you're using without a damn good reason to do so, so whoever wrote this function was probably pretty bone-headed, assuming it's not simply a bug.

也就是说,通常不建议在没有充分理由的情况下反对您正在使用的任何语言或框架的习语,因此编写此函数的人可能非常愚蠢,假设它不仅仅是一个错误。

回答by Kelend

It may very well be a mistake on the original author, however the notion that 1 is true and 0 is false is not a universal concept. In shell scripting 0 is returned for success, and any other number for failure. In other languages such as Ruby, only nil and false are considered false, and any other value is considered true, so in Ruby both 1 and 0 would be considered true.

这很可能是原作者的错误,但是 1 为真而 0 为假的概念并不是一个普遍的概念。在 shell 脚本中,成功返回 0,失败返回任何其他数字。在 Ruby 等其他语言中,只有 nil 和 false 被认为是 false,任何其他值都被认为是 true,因此在 Ruby 中 1 和 0 都被认为是 true。

回答by user541686

I'm not sure if I'm answering the question right, but here's a familiar example:

我不知道如果我回答问题的权利,但这里有一个熟悉的例子:

The return type of GetLastError()in Windows is nonzero if there was an error, or zero otherwise. The reverse is usually true of the return value of the function you called.

GetLastError()如果出现错误,Windows 中的返回类型为非零,否则为零。您调用的函数的返回值通常正好相反。