In C ~, an operator that performs a bit-muddy shift operation complements the original number. And in C there is no boolean type, 0 is false, everything else is true, in your example:
while(~scanf("%d",&n)) { }
scanf () returns EOF if there are no more characters to read, which is -1 (not on all platforms!), so -1 is represented (111 ... 32 times in a 32-bit architecture) ~ EOF = 0 in this way risk that code does not work for all platforms.
source share