C Library Functions -atol()

C Standard Library - <stdlib.h>

Description

C Library Functionslong int atol(const char *str)Pass the parameterstrThe string pointed to is converted into a long integer (of type long int).

Declaration

The following is the declaration of the atol() function.

long int atol(const char *str)

Parameter

  • str-- the string to be converted to a long integer.

Return Value

This function returns the converted long integer; if no valid conversion is performed, it returns zero.

Example

The following example demonstrates the usage of the atol() function.

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

int main()
{
   long val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atol(str);
   printf("字符串值 = %s, 长整型值 = %ld\n", str, val);

   strcpy(str, "example.com");
   val = atol(str);
   printf("字符串值 = %s, 长整型值 = %ld\n", str, val);

   return(0);
}

Let us compile and run the above program, which will produce the following result:

字符串值 = 98993489, 长整型值 = 98993489
字符串值 = example.com, 长整型值 = 0

C Standard Library - <stdlib.h>

other extensions