Use strtol (or strtoll, strtoul, strtod) from <stdlib.h> to convert a string to a number in C, and check the end pointer and errno to detect bad input. Unlike atoi, which returns 0 for both "0" and "abc" and has undefined behaviour on overflow, strtol tells you exactly where parsing stopped and whether the value fit. If you searched for "strtol atoi in c", the short rule is: use atoi only for throwaway code, and strtol everywhere else.
This guide assumes you already know how C strings are stored as null-terminated char arrays.
atoi converts the leading digits of a string and returns an int. It gives you no way to tell failure from a real zero:
The C standard says that if the result cannot be represented, the behaviour of atoi is undefined. That makes it a poor fit for anything that reads user input, files or network data.
The signature is:
nptr is the string to parse. Leading whitespace is skipped, then an optional + or - sign is read.endptr, if not NULL, is set to point at the first character that was not part of the number.base is 2 to 36, or 0 to auto-detect: a 0x prefix means hexadecimal, a leading 0 means octal, anything else is decimal.The function returns the parsed value. If no digits were found, it returns 0 and sets *endptr to nptr. If the value is out of range for long, it returns LONG_MAX or LONG_MIN and sets errno to ERANGE.
A correct check has three parts: clear errno before the call, compare endptr with the start of the string, and make sure the whole string was consumed.
Setting errno = 0 first matters because library functions only set errno on failure; they never reset it. The long range check matters because long is 32 bits on Windows and 64 bits on most Linux and macOS systems, so the same input can behave differently across platforms.
Because endptr tells you where parsing stopped, you can read several numbers in one pass:
This works because strtol skips leading whitespace, so you can pass end straight back in.
| Function | Returns | Header | Notes |
|---|---|---|---|
strtol | long | <stdlib.h> | Signed, ERANGE on overflow |
strtoll | long long | <stdlib.h> | At least 64 bits |
strtoul | unsigned long | <stdlib.h> | Accepts a leading - and wraps around, so check for it yourself |
strtoull | unsigned long long | <stdlib.h> | Same caveat as strtoul |
strtod | double | <stdlib.h> | Also sets ERANGE; accepts inf and nan |
Passing 0 as the base is handy for configuration values:
Remember that a leading zero means octal when the base is 0, so "010" is 8, not 10. If you only want decimal, pass 10.
It is not removed from the standard, but its behaviour on overflow is undefined and it cannot report errors, so most style guides tell you to avoid it. Use strtol instead.
strtol only writes errno when the value is out of range. If errno already held an old error from earlier code, you would wrongly believe this call failed.
Use strtod with the same pattern: reset errno, pass an endptr, and check that end != s and *end == '\0'. atof has the same weaknesses as atoi.
No. A trailing \n makes *end non-zero. Either strip the newline first or allow trailing whitespace in your check.
atoi hides errors; strtol exposes them. Clear errno, call strtol with an endptr and an explicit base, then reject the input if no digits were read, if characters remain, or if errno is ERANGE. Narrow the result to int only after a range check.