strtol() function in c

What is strtol in c?

strtol converts the initial part of the given string to long int value according to the base value.

The base value must be within 2 to 36.

It is defined under stdlib.h header file.

Prototype

long int strtol(const char*string,char **endpointer,int base);

Return value

The function returns converted long int value else zero is returned.


Example program for strtol() in c

#include<stdio.h>
#include<stdlib.h>
int main()
{
 char string[30]=" 40.30   scholarsoul";
  char *pointer;
  double res;
  res=strtol(string,&pointer,10);
  printf("The number is:%lf and string is:%s \n",res,pointer);
  return 0;
}

Output

 ./a.out 
The number is:40.000000 and string is:.30   scholarsoul 
Output of strtol in c programming

Code explanantion

It converts the initial part of the given string to long int value according to the base value.


People also ask for

What is the difference between atol and strtol in c?

atol lead to undefined behavior in case of overflow. strol gives flexibility.


People also read


Leave a Comment