我有一个简单的问题,我正在学习C编程,我知道!运算符表示逻辑非。我的问题是,在这种情况下,以“做而做”循环为条件是什么意思?
是否以某种方式检查变量是否已更改?
我知道我应该以某种方式找到它,但是当我尝试使用Google'!'时,它并不想显示我的意思。
#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
与ok == 0
相同。
请记住,在C中,布尔上下文中的任何非零标量值都表示“ true”,而零表示“ false”。编写!foo
而不是foo == 0
是常见的C习惯用法。它也适用于指针:
FILE *foo = fopen( "some_file", "r" );
if ( !foo )
{
fprintf( stderr, "could not open some_file\n" );
return EXIT_FAILURE;
}
因此,
while ( x )
与while ( x != 0 )
相同,并且while ( !x )
与while ( x == 0 )
相同。关于c - 什么!运算符的意思是如果放在变量前面?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57930618/