c - how does scanf() check if the input is an integer or character? -


i wondering how standard c library function scanf() check if input integer or character when call scanf("%d",&var) when character number? know when encounters non-integer puts input buffer , returns -1 how know input not integer?

you're correct in each character represented 8-bit integer. solution simple: @ number, , see if in range 48-57, range of scii codes characters '0' - '9'.

starting on line 1315 of scanf() source code can see in action. scanf() more complicated, though - looks @ multi-byte characters determine numeric value. line 1740 magic happens , character converted number. finally, , possibly useful, strtol() function looping perform conversion.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -