atol() — atoll() — Convert Character String to Long or Long Long Integer

Format (atol())

#include <stdlib.h>
long int atol(const char *string);

Format (atoll())

#include <stdlib.h>
long long int atoll(const char *string);

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of these functions might be affected by the LC_CTYPE category of the current locale. For more information, see Understanding CCSIDs and Locales.

Description

The atol() function converts a character string to a long value. The atoll() function converts a character string to a long long value.

The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character can be the null character that ends the string.

The atol() and atoll() functions do not recognize decimal points or exponents. The string argument for this function has the form:

Read syntax diagramSkip visual syntax diagram>>-+------------+--+----+--digits------------------------------><
   '-whitespace-'  +-+--+
                   '- --'
 

where whitespace consists of the same characters for which the isspace() function is true, such as spaces and tabs. The atol() and atoll() functions ignore leading white-space characters. The value digits represents one or more decimal digits.

Return Value

The atol() and atoll()functions return a long or a long long value that is produced by interpreting the input characters as a number. The return value is 0L if the function cannot convert the input to a value of that type. The return value is undefined in case of overflow.

Example that uses atol()

This example shows how to convert numbers that are stored as strings to numeric values.

#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
    long l;
    char *s;
 
    s = "98854 dollars";
    l = atol(s);     /* l = 98854 */
 
    printf("l = %.ld\n",l);
}
 
/*******************  Output should be similar to:  ***************
 
l = 98854
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]