Use C strtol function to convert string to long

Syntax

C strtol function has the following syntax.

long double strtol(const char * restrict start, char restrict ** restrict end);

C strtol function is from header file stdlib.h.

Description

C strtol function converts the string into a long and returns the result.

Example

Use C strtol function to convert string to long.


#include <stdio.h>
#include <stdlib.h>
/*from w ww.  ja v  a  2  s  .  co m*/
int main()
{                                 
   const char *string = "-1234567abc"; 
   
   char *remainderPtr; 
   long x;             
   
   x = strtol( string, &remainderPtr, 0 );

   printf( "%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
           "The original string is ", string,
           "The converted value is ", x,
           "The remainder of the original string is ", 
           remainderPtr,
           "The converted value plus 567 is ", x + 567 );

   return 0; 

}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h