Syntax
#include <stdlib.h> long int atol(const char *string);Description
atol converts a character string to a long value.
The input string is a sequence of characters that can be interpreted as a numerical 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.
atol does not recognize decimal points nor exponents. The string argument for this function has the form:
┌──────────────────────────────────────────────────────────────────────────────┐
│                                        │
│ >>──┬────────────┬──┬───┬──digits──><                     │
│   └─whitespace─┘  ├─+─┤                           │
│           └─┴─┘                           │
│                                        │
└──────────────────────────────────────────────────────────────────────────────┘
where whitespace consists of the same characters for which the isspace
function is true, such as spaces and tabs. atol ignores leading white-space
characters. digits is one or more decimal digits.
atol returns a long value 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.
This example shows how to convert numbers stored as strings to numerical values using the atol function.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   long l;
   char *s;
   s = "98854 dollars";
   l = atol(s);                                           /* l = 98854        */
   printf("atol( %s ) = %d\n", s, l);
   return 0;
   /****************************************************************************
      The output should be similar to :
      atol( 98854 dollars ) = 98854
   ****************************************************************************/
}
Related Information