atol : <stdlib.h> library function

The function long int atol(const char *str); converts a string to a long integer(long int). The function skips all white-space characters at the beginning of the string until the first non-whitespace character is found. Then it converts the subsequent characters into a long integer number until it finds a character which is not a digit. It also takes an optional initial plus or minus sign before first base-10 digit in string.

Function prototype of atol

long int atol(const char *str);
  • str : A pointer to a string beginning with the representation of an long integer number.

Return value of atol

The function returns the converted integer number as a long int value otherwise it returns zero(0), If no valid conversion could be performed.

C program to show the use of atol function

The following program shows the use of atol function for string to long integer conversion.

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

int main(){
    char string[100];
    long value;
    printf("Enter a string\n");
    scanf("%s", &string);
    
    /* String to Long integer conversion */
    value = atol(string);
    printf("Long value %ld\n", value);
     
    return 0;
}

Program Output
Enter a string
1234
Long value 1234
Enter a string
1234.555
Long value 1234
Enter a string
1234ABCD
Long value 1234
Enter a string
ABCD
Long value 0