〜(代字号)在c ++中的scanf函数之前意味着什么?

What does ~(tilde) means before scanf function in c++?

我找到了一个接受输入字符串并将其打印出来的代码。

但是我不知道scanf前面的波浪号是什么意思

我发现 tilde 可用于析构函数或二进制否定,但它看起来不像两者。如果没有波浪号,代码将无法工作。


int main() {
    char arr;

    while (~scanf("%c", &arr)){
        putchar(arr);
    }
}

I found tilde can be used for either destructor or binary negation but it doesn't look like both.

这是bitwise NOT operator 应用于 scanf() 的 return 值,正如你在后面提到的。

And the code doesn't work without tilde.

:

scanf returns the number of values it scanned successfully or EOF if it reaches the end of file. EOF is a macro that represents a negative value. On most platforms, the value of EOF is (int) -1. In this case, taking 1's complement of -1, will make the value as 0 and is used to break from the loop.