Hi

What are the basic differences between the methods atol() and strtol()?

Regards,
Amit

Dani AI

Generated

Good points from and — here is a compact, practical guide that fills in the error‑handling and parsing details you need to make conversions robust.

atol is simple but limited: it behaves like calling strtol with a NULL end pointer and base 10, it does not reliably report errors, and it is not suitable when you need to detect invalid input or overflow. See the library doc for the limitations and the recommendation to prefer the strto* family. (atoi/atol man page).

Use strtol (or strtoll/strtoul) with endptr and errno. Typical pattern:

#include <errno.h>
#include <limits.h>
#include <stdlib.h>

/* returns 1 on success, 0 on error */
int parse_long(const char *s, long *out) {
    char *end;
    errno = 0;                    /* clear errno before call */
    long val = strtol(s, &end, 10);/* use base 10 (or 0 to auto-detect 0x/0 prefix) */

    if (s == end)                 /* no digits found */
        return 0;
    if ((val == LONG_MAX || val == LONG_MIN) && errno == ERANGE) /* overflow/underflow */
        return 0;
    if (*end != '\0')             /* trailing non-numeric data (optional) */
        return 0;

    *out = val;
    return 1;
}

The rules above come from the standard/POSIX behavior for strtol: set errno to 0 before the call, check endptr to detect "no conversion", test ERANGE and LONG_MAX/LONG_MIN for out‑of‑range, and check for trailing characters if that matters. (strtol POSIX man page, cppreference strtol).

Notes and modern tips: reset errno before each call; pick strtoll/strtoull when the value may exceed long; prefer unsigned versions for unsigned targets; and if writing C++ and you want a non‑throwing, locale‑independent, zero‑allocation parser, consider std::from_chars (C++17). For secure code, avoid atoi/atol — use strto* or from_chars. (std::from_chars)

Summary: follow 's advice — use strtol unless input is already validated — and use the endptr trick mentioned to parse prefixes or detect partial conversions.

Recommended Answers

All 5 Replies

strtol offers more error handling capability and is safer. atol is easier to use, but is impossible to properly check for errors and exhibits undefined behavior for some erroneous input. The basic difference is that you should use strtol unless your string has already been carefully validated and you're 100% certain that the conversion will succeed with atol.

strtol takes an extra parameter that can be set to what array-element it should convert.
So you could only convert the first 3 chars to a long, or 10 or the middle 5 or ... etc.
atol does not have this functionality.

[edit] And what Narue says is also true, because she's never wrong ;)

Thanks friends for your responses.

strtol offers more error handling capability and is safer. atol is easier to use, but is impossible to properly check for errors and exhibits undefined behavior for some erroneous input.

How can I handle the errors in case of strtol() ?


Amit.

>How can I handle the errors in case of strtol() ?
strtol sets errno, it returns suitable values on error, and one of the parameters is a pointer that marks where the conversion stopped. You can mix and match these three error handling schemes to write a bullet-proof conversion.

Thanx a lot Narue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.