Easy Tutorial
❮ C Exercise Example40 C Function Localtime ❯

C Library Function - atol()

C Standard Library - <stdlib.h>

Description

The C library function long int atol(const char *str) converts the string pointed to by str into a long integer (type long int).

Declaration

Below is the declaration for the atol() function.

long int atol(const char *str)

Parameters

Return Value

The function returns the converted long integer, or zero if no valid conversion could be performed.

Example

The following example demonstrates the use 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("String value = %s, Long integer value = %ld\n", str, val);

   strcpy(str, "tutorialpro.org");
   val = atol(str);
   printf("String value = %s, Long integer value = %ld\n", str, val);

   return(0);
}

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

String value = 98993489, Long integer value = 98993489
String value = tutorialpro.org, Long integer value = 0

C Standard Library - <stdlib.h>

❮ C Exercise Example40 C Function Localtime ❯