C atol() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C atol() function converts decimal text to long. It skips leading white space and accepts an optional sign before decimal digits. It is convenient only when input validity and range are already guaranteed.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. A valid in-range prefix produces its long value. Invalid or out-of-range input cannot be separated reliably from ordinary values; use strtol(). The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete atol() call:

C
atol("-1200");
CallWhat it does
atol("-1200");Converts decimal text to long using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
long atol(const char *str);

Parameters

ParameterDescription
strNull-terminated byte string containing a decimal integer.

Return Value

Return ValueDescription
long valueThe converted decimal value.
0LEither a valid zero or an unreported conversion problem.

C Examples

Example 1 — Convert known text

This complete program demonstrates a distinct part of the atol() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    const char *text = "42";
    long value = atol(text);
    printf("%ld\n", value);
    return 0;
}

How It Works

  1. Step 1. Store the known input as a C string.
  2. Step 2. Pass it to atol().
  3. Step 3. The function accepts the numeric spelling.
  4. Step 4. Print the long result; validated input should use strtol instead.

Example 2 — Accept a leading sign

This complete program demonstrates a distinct part of the atol() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    const char *text = "-17";
    long value = atol(text);
    printf("%ld\n", value);
    return 0;
}

How It Works

  1. Step 1. Store the known input as a C string.
  2. Step 2. Pass it to atol().
  3. Step 3. The function accepts the numeric spelling.
  4. Step 4. Print the long result; validated input should use strtol instead.

Example 3 — Observe prefix conversion

This complete program demonstrates a distinct part of the atol() contract and checks the result or precondition that matters for the scenario.

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

int main(void) {
    const char *text = "120items";
    long value = atol(text);
    printf("%ld\n", value);
    return 0;
}

How It Works

  1. Step 1. Store the mixed input as a C string.
  2. Step 2. Pass it to atol().
  3. Step 3. The function stops after the numeric prefix.
  4. Step 4. Print the long result; validated input should use strtol instead.

Frequently Asked Questions

atol() converts decimal text to long. It skips leading white space and accepts an optional sign before decimal digits. It is convenient only when input validity and range are already guaranteed.
Include <stdlib.h> before calling atol().
A valid in-range prefix produces its long value. Invalid or out-of-range input cannot be separated reliably from ordinary values; use strtol().
atol() has no dependable error channel. Prefer strtol() with endptr and errno for input that must be checked.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: atoll()

Continue through the C stdlib.h tutorials.

atoll() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful