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.
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:
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:
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.