C语言 逻辑否定在 C 中如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2319766/
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 04:35:27  来源:igfitidea点击:

How does logical negation work in C?

c

提问by Ben Fossen

I have been using ! (logical negation) in C and in other languages, I am curious does anyone know how to make your own ! function? or have a creative way of making one?

我一直在用!(逻辑否定)在 C 和其他语言中,我很好奇有没有人知道如何制作自己的!功能?或者有一种创造性的制作方式?

回答by Hans W

int my_negate(int x)
{
    return x == 0 ? 1 : 0;
}

回答by Pascal Cuoq

!ecan be replaced by ((e)?0:1)

!e可以替换为 ((e)?0:1)

回答by t0mm13b

Remember the bang operator '!' or exclamation mark in english parlance, is built into the programming language as a means to negate.

记住 bang 运算符 '!' 或英语中的感叹号,是内置于编程语言中的一种否定方式。

Consider this ternary operator example:

考虑这个三元运算符示例:

(some condition) ? true : false;

Now, if that was negated, the ternary operator would be this

现在,如果这被否定,三元运算符将是这个

(some condition) ? false : true;

The common area where that can get some programmers in a bit of a fit is the strcmpfunction, which returns 0 for the strings being the same, and 1 for two strings not the same:

可以让一些程序员有点适应的公共区域是strcmp函数,对于相同的字符串返回 0,对于两个不同的字符串返回 1:

if (strcmp(foo, "foo")){

}

When really it should be:

真正应该是:

if (!strcmp(foo, "foo")){
}

In general when you negate, it is the opposite as shown in the ternary operator example...

一般来说,当你否定时,它是相反的,如三元运算符示例所示......

Hope this helps.

希望这可以帮助。

回答by bta

C considers all non-zero values "true" and zero "false". Logical negation is done by checking against zero. If the input is exactly zero, output a non-zero value; otherwise, output zero. In code, you can write this as (input == 0) ? 1 : 0(or you can convert it into an ifstatement).

C 认为所有非零值“真”和零“假”。逻辑否定是通过检查零来完成的。如果输入正好为零,则输出一个非零值;否则,输出零。在代码中,您可以(input == 0) ? 1 : 0将其编写为(或者您可以将其转换为if语句)。

When you ask how to "make your own ! method", do you mean you want to write a function that negates a logic value or do you want to define what the exclamation-point operator does? If the former, then the statement I posted above should suffice. If the latter, then I'm afraid this is something that can't be done in C. C++ supports operator overloading, and if doing this is a strict necessity then I would suggest looking there.

当您问如何“创建自己的 ! 方法”时,您的意思是要编写一个否定逻辑值的函数,还是要定义感叹号运算符的作用?如果是前者,那么我上面发布的声明就足够了。如果是后者,那么恐怕这在 C 中是无法完成的。C++ 支持运算符重载,如果这样做是绝对必要的,那么我建议在那里查看。

回答by thebretness

If you want to overload an operator, the proper prototype is:

如果要重载运算符,正确的原型是:

bool operator!();

I'm not a big fan of overloading operators, but some people like their syntactic sugar. EDIT: This is C++ only! Put it in the definition of your class.

我不是重载运算符的忠实粉丝,但有些人喜欢他们的语法糖。编辑:这只是 C++!把它放在你的类的定义中。