You have 3 options:
This is probably the fastest if you use it in performance-critical code, but it does not report errors. If the string does not start with an integer, it will return 0. If the string contains garbage after the integer, it converts the initial part and ignores the rest. If the number is too large to match int , no behavior is specified.
sscanf
Some error messages, and you have a lot of flexibility for what type to store (signed / unsigned versions of char/short/int/long/long long/size_t/ptrdiff_t/intmax_t ).
The return value is the number of successful conversions, so scanning for "%d" will return 0 if the string does not start with an integer. You can use "%d%n" to store the index of the first character after an integer that is read in another variable, and thus check whether the entire string has been converted or if there is unwanted information after that. However, like atoi , integer overflow behavior is not specified.
strtol and family
Reliable error reporting if you set errno to 0 before making a call. Return values are indicated when overflowing and errno will be set. You can choose any base of numbers from 2 to 36 or specify 0 as the base for automatic interpretation of leading 0x and 0 as hexadecimal and octal, respectively. The choice of type for conversion is the signed / unsigned versions of long/long long/intmax_t .
If you need a smaller type, you can always save the result in a temporary variable long or unsigned long and check the overflow yourself.
Since these functions take a pointer to a pointer argument, you also get a pointer to the first character following the converted integer, for free, so you can determine if the whole line was an integer or analyze the subsequent data in the line if necessary.
Personally, I would recommend the strtol family for most purposes. If you do something fast and dirty, atoi can satisfy your needs.
Aside, sometimes I find that I need to parse numbers, where leading spaces, signs, etc. should not be accepted. In this case, it’s quite easy to scroll your own loop, for example,
for (x=0; (unsigned)*s-'0'<10; s++) x=10*x+(*s-'0');
Or you can use (for reliability):
if (isdigit(*s)) x=strtol(s, &s, 10); else