Does sscanf ("123456789123456789123456789", "% d", & n) have specific behavior?

If sscanf() or another function from the scanf family is given a sequence of digits whose converted value exceeds the maximum value of the target integer type,

  • Should the conversion be considered unsuccessful?
  • Is this behavior defined at all?
+42
c undefined-behavior scanf
Jun 24 '17 at 19:58
source share
1 answer

From the standard 7.21.6.2p10 ( (f)scanf applies to the whole family):

... If this object does not have the appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined .

Looks like another reason to be very careful with the scanf family. The strtoXX functions have fully defined behavior. They return LONG_MAX , etc. For input too large and set errno == ERANGE . Therefore, if you need accurate information, enter the token manually and use these functions to convert. Another advantage: improved error handling.

+46
Jun 24 '17 at 20:14
source share



All Articles