Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. atoi() / atof() / atol() / strtol()

atoi() / atof() / atol() / strtol()

These functions convert a string representation of a number into a numeric type you can use in your program. They are commonly used when processing strings read from command-line arguments or files as numeric values.

Syntax

// Converts a string to int. Returns 0 if conversion fails.
int atoi(const char *str);

// Converts a string to double.
double atof(const char *str);

// Converts a string to long.
long atol(const char *str);

// Converts a string to long. Lets you specify a pointer to where conversion stopped and the numeric base.
// endptr: pointer to the position where conversion ended (pass NULL if not needed). base: numeric base (2–36, or 0 for auto-detection).
long strtol(const char *str, char **endptr, int base);

// Converts a string to double (with error detection support).
double strtod(const char *str, char **endptr);

Function Comparison

FunctionReturn TypeError DetectionBase Specification
atoi()intNot supported (cannot distinguish from 0).Decimal only.
atol()longNot supported (cannot distinguish from 0).Decimal only.
atof()doubleNot supported (cannot distinguish from 0.0).Decimal only.
strtol()longSupported (check via endptr).2–36, or auto-detect.
strtod()doubleSupported (check via endptr).Decimal only.

Sample Code

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    // Convert a string to int using atoi.
    int n = atoi("42");
    printf("atoi: %d\n", n); // Prints "atoi: 42".

    // Convert a string to double using atof.
    double d = atof("3.14");
    printf("atof: %.2f\n", d); // Prints "atof: 3.14".

    // Convert a string using strtol with error detection.
    char *end;
    long val = strtol("  -123abc", &end, 10);
    printf("strtol: %ld\n", val); // Prints "strtol: -123".
    printf("remaining: %s\n", end);    // Prints "remaining: abc".

    // Convert a hexadecimal string using strtol.
    long hex_val = strtol("0xFF", NULL, 16);
    printf("0xFF = %ld\n", hex_val); // Prints "0xFF = 255".

    // With base 0, strtol auto-detects: "0x" as hex, "0" as octal.
    long auto_val = strtol("0755", NULL, 0);
    printf("0755(octal) = %ld\n", auto_val); // Prints "0755(octal) = 493".

    // Detecting a conversion failure (not possible with atoi).
    char input[] = "abc";
    end = input;
    long result = strtol(input, &end, 10);
    if (end == input) {
        printf("Conversion failed: not a number.\n"); // This line is printed.
    }

    return 0;
}

Notes

atoi() / atol() / atof() do not support error detection. When conversion fails, they simply return 0 or 0.0, making it impossible to determine whether the input was invalid. In situations where reliability matters, use strtol() or strtod() and check the conversion endpoint via endptr.

If an overflow occurs, strtol() returns LONG_MAX or LONG_MIN and sets errno to ERANGE. When handling external input such as command-line arguments, be sure to check errno as well.

For the reverse conversion (number → string), use sprintf() / snprintf().

If you find any errors or copyright issues, please .