Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdlib.h

C atoi(), atol() and atof() functions

Reference for C atoi(), atol(), atoll() and atof(): convert a string to a number the simple way, what they return on bad input and why strtol is safer.

atoi(), atol() and atoll() convert the beginning of a string to an int, long and long long. atof() converts to a double. They skip leading white space, read an optional sign and as many digits as they can, and ignore everything after that.

They are short and handy for quick scripts, but they cannot tell you whether a conversion worked: a string like "abc" and a string like "0" both give 0, and an out-of-range value is undefined behavior. For anything that reads user or file input, use strtol() and strtod() instead.

C
#include <stdlib.h>

int       atoi(const char *s);
long      atol(const char *s);
long long atoll(const char *s);
double    atof(const char *s);

Try it

C What atoi accepts and ignores

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C atof, and the failure you cannot detect

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int       atoi(const char *s);
long      atol(const char *s);
long long atoll(const char *s);
double    atof(const char *s);
Name Description
s A null-terminated string. Leading white space is skipped. For atof() the usual floating-point forms are accepted: decimal digits with an optional fraction and exponent, and in practice also inf, nan and hexadecimal floats.

Returns: the converted value. If no conversion can be made, the result is 0. The functions behave like strtol(s, NULL, 10) (or strtod), except for error reporting.

Errors / undefined behavior: if the value does not fit the result type, the behavior is undefined (glibc returns a wrapped or clamped value, but you cannot count on it). A NULL or unterminated string is undefined behavior. Nothing sets errno reliably.

Usage notes

  • Zero is ambiguous. atoi("0") and atoi("zero") both return 0. If 0 is a legal input, the functions cannot be used safely.
  • Trailing junk is ignored. atoi("12abc") is 12 and atoi("3.99") is 3. A validating parser must detect leftover characters, which only strtol()'s end pointer provides.
  • Only base 10. atoi("0x1F") is 0. Use strtol(s, &end, 16) or base 0 for prefixes. (Note: on this runner's glibc strtol ignores a 0b prefix, so binary strings need base 2 and the prefix removed.)
  • Overflow is undefined, unlike strtol(), which returns LONG_MAX or LONG_MIN and sets errno to ERANGE.
  • atof() and locale: the decimal point follows the current locale ("3,5" may work in a German locale). The default "C" locale uses a dot.
  • Speed: all of them are fast; the safety difference matters, not performance.
  • Where it is acceptable: command-line tools where the input is generated by your own program, tests and throw-away code.
  • C++ and Java readers: there is no exception for a bad string; the functions just return zero.