Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

atol — C23 library function

`atol(const char *nptr)` from `<stdlib.h>` reads an initial decimal integer and returns `long int`. It skips white space, accepts an optional sign, and stops at the first character outside the number.

Version: C23

Syntax

long int atol(const char *nptr);

atol(const char *nptr) from <stdlib.h> reads an initial decimal integer and returns long int. It skips white space, accepts an optional sign, and stops at the first character outside the number.

It fits previously validated counters consumed by interfaces whose type is long. The example actually converts "+123456ms" to 123456 and prints the result with the correct %ld format.

The width of long is platform-dependent and can be only 32 bits. No digits gives an ambiguous 0, and a result outside LONG_MIN..LONG_MAX has undefined behavior in C23; nptr must not be NULL. strtol provides end and range checking.

Examples

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

int main(void) {
    long count = atol("+123456ms");
    printf("%ld\n", count);
    return 0;
}

Result

123456

It fits previously validated counters consumed by interfaces whose type is `long`. The example actually converts `"+123456ms"` to `123456` and prints the result with the correct `%ld` format.

Sources