Parsing numbers


Converting a string to a number is a common programming task. We will review how to do this task in C, and focus on the common case of parsing integers.

Old/Bad way (atoi)

The old way to parse integers is the library function atoi ("ASCII string to int), and its related functions, atol, atoll, and atof, which convert a string to a long, long long, and double, respectively.

The following program demonstrates the use of atoi:



      

You will see atoi used a lot in old code, but it has one major and one minor problem:

  1. Major: atoi does not detect errors: it returns 0 if its input string does not represent a number, and -1 if the string does represent a number, but the number is too small or too large to represent as an int.
  2. Minor: atoi only handles strings that represent decimal numbers; it does not handle hexadecimal or octal representations.
To account for these problems, POSIX invented a new family of functions ...

New/Good way (strtol)

The strtol (and it's relatives strtoll (long long), strtoul (unsigned long), strtoull (unsigned long long), ) rectifies the deficiencies of atoi at the expense of a slightly more complicated interface.

The following is the same program as before, now written with strtol. Note the following:



      

Using strtol to parse other integer types

Once you get the hang of strtol, it's straightforward to use the constants in limits.h, and some careful casting, to parse strings to other integer types, such as int or short.

The following program wraps strtol in a helper function, mu_str_to_long, and then demonstrates a function, mu_str_to_int, which leverages mu_str_to_long to convert a string to an int. In other words, mu_str_to_int is a "safe" version of atoi.