Easy Tutorial
❮ C Operators C Exercise Example4 ❯

C Library Function - strtol()

C Standard Library - <stdlib.h>

Description

The C library function long int strtol(const char str, char *endptr, int base) converts the initial part of the string pointed to by str to a long integer value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

Declaration

Here is the declaration for the strtol() function.

long int strtol(const char *str, char **endptr, int base)

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 strtol() function.

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

int main()
{
   char str[30] = "2030300 This is test";
   char *ptr;
   long ret;

   ret = strtol(str, &ptr, 10);
   printf("The number (unsigned long integer) is %ld\n", ret);
   printf("The string part is |%s|", ptr);

   return(0);
}

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

The number (unsigned long integer) is 2030300
The string part is | This is test|

C Standard Library - <stdlib.h>

❮ C Operators C Exercise Example4 ❯