c - Using the return value of "scanf()" to check for end of file -


i searching net on how use return value of scanf check end of file! found following code.but having difficulty in understanding?

how method working?

what '~' operator signify?

while(~scanf("%d",&n)) {    /* solution */ } 

this horrible way check if value different -1. ~x returns bitwise negation of x. having in mind complimentary code used negative numbers(on most compilers way approach not portable) -1 represented sequence of 1-s , ~(-1) produce zero.

please don't use approach. write scanf("%d", &n) != eof way easier understand.


Popular posts from this blog

Php - Delimiter must not be alphanumeric or backslash -

c# - How to change the "Applies To" field under folder auditing options programatically (.NET) -

c++ - Ambiguity when using boost::assign::list_of to construct a std::vector -