Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

atoi — C23 library function

`atoi(const char *nptr)` from `<stdlib.h>` converts an initial decimal integer in a string and returns `int`. It skips leading white space, accepts an optional sign, and stops at the first character that does not belong to the number.

Version: C23

Syntax

int atoi(const char *nptr);

atoi(const char *nptr) from <stdlib.h> converts an initial decimal integer in a string and returns int. It skips leading white space, accepts an optional sign, and stops at the first character that does not belong to the number.

Use it only for small, previously validated configuration values. The example actually passes " -42items", so it obtains -42 and ignores the items suffix.

No digits returns 0, indistinguishable from a valid zero. If the result cannot be represented as int, C23 makes behavior undefined, and nptr must not be NULL. For untrusted text use strtol and check the end pointer, errno, and INT_MIN..INT_MAX.

Examples

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

int main(void) {
    int count = atoi("  -42items");
    printf("%d\n", count);
    return 0;
}

Result

-42

Use it only for small, previously validated configuration values. The example actually passes `" -42items"`, so it obtains `-42` and ignores the `items` suffix.

Sources