What does “~scanf” mean?

  • A+

I saw it in a code like this


I know that scanf() has return value,but I can’t understand what does ~ mean


This is a silly trick that relies on EOF having all its bits set. Since the standard does not guarantee the exact value of EOF, the behavior of this program is platform-dependent.

When scanf detects end-of-input, it returns EOF. Standard requires EOF to be negative. Very often EOF is set to -1. When ~ is applied to -1, you get back a zero, so the loop stops. On platforms with EOF defined as some other negative number the loop will never stop. Code's behavior also depends on the implementation-defined behavior of ~ with signed values.

You should rewrite the loop as follows:

while (scanf("%d", &a) != EOF) {     ... } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: