简体   繁体   English

是什么? 运算符意味着在变量前面?

[英]What does a ! operator mean in front of a variable?

I have just a simple question, I'm learning C programming and I know that the.我只有一个简单的问题,我正在学习 C 编程并且我知道。 operator means logical NOT?运算符表示逻辑非? My question is that what does it mean in this case in a do while cycle as a condition?我的问题是,在这种情况下,以 do while 循环作为条件意味着什么? Is it checking somehow if the variable is changed?它是否以某种方式检查变量是否已更改?

I know I should probably find it somehow, but when I try to google '.'s it doesn't want to show what I meant.我知道我可能应该以某种方式找到它,但是当我尝试用谷歌搜索“.”时,它不想显示我的意思。

#include <stdio.h>

int main() {
    int input, ok=0;
    do {
        scanf("%d", &input);
        if ( /* condition */) {
            //the input is wrong it will scan another one
        }
        else {
            //the input is correct, so the while cycle can stop
            //we don't need more inputs
            ok = 1;
        }
    } while (!ok);

    //...
    //...
    //...

    return 0;
}

!ok is the same as ok == 0 . !okok == 0相同。

Remember that in C, any non-zero scalar value in a Boolean context means "true" while zero means "false".请记住,在 C 中,布尔上下文中的任何非零标量值都表示“真”,而零表示“假”。 It's a common C idiom to write !foo instead of foo == 0 .!foo而不是foo == 0是 C 的一个常见习惯用法。 It works for pointers as well:它也适用于指针:

FILE *foo = fopen( "some_file", "r" );
if ( !foo )
{
  fprintf( stderr, "could not open some_file\n" );
  return EXIT_FAILURE; 
}

So, while ( x ) is the same as while ( x != 0 ) , and while ( !x ) is the same as while ( x == 0 ) .因此, while ( x )while ( x != 0 )相同,而while ( !x )while ( x == 0 )相同。

Speaking loosely, the given code executes the contents of the loop repeatedly "while NOT ok", meaning "while OK is zero".松散地说,给定的代码重复执行循环的内容“while NOT ok”,意思是“while OK is zero”。 In the else block, ok is set to 1, meaning that the loop will not repeat again.else块中, ok被设置为 1,表示循环不会再次重复。

More formally, !ok is an expression that is true when ok is equal to zero, and false when ok is any value other than zero.更正式地说, !ok是一个表达式,当ok等于零时为真,当ok为非零的任何值时为假。

In the context of logical operations, anything except zero (*) is true , zero is false .在逻辑运算的上下文中,除零 (*) 之外的任何内容都是true ,零是false

so !1 is "! true" is false所以!1是 "!true" 是false
!0 is "! false" is true !0为 "!false" 为true

(*) any kind of zero: NULL counts as zero, 0.0 counts as zero (*) 任何类型的零: NULL算作零, 0.0算作零

Just think about it, the ok has a Boolean value, which is false at the initialisation.想想看,ok 有一个布尔值,在初始化时是 false。

(In c, 0 has a false value, I assume that you know this.) (在 c 中,0 有一个假值,我假设你知道这一点。)

In the do while, it is simply negates the logical value, it still means a NOT.在 do while 中,它只是对逻辑值取反,它仍然意味着一个 NOT。 If the condition is true, it will loop, otherwise, won't.如果条件为真,它将循环,否则,不会。

If ok has a false value and you'll negate it, it will have a true value, the loop will, umm loop.如果 ok 有一个 false 值并且你将否定它,它将有一个 true 值,循环将,umm 循环。

I hope you'll get the point, it does not have any other meanings.我希望你能明白这一点,它没有任何其他含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM