Approach-1:
- We use strtol() function to covert string to long integer.
- It is belongs to stdlib.h header file.
- The prototype of function is
- long int strtol(const char *str, char **endptr, int base)
- It converts the initial part of the string in str to a long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.
#include<stdio.h>
#include<stdlib.h>
int main ()
{
char s[] = "2021 1a 1101";
char *p;
long int a, b, c;
a = strtol (s,&p,10);
b = strtol (p,&p,16);
c = strtol (p,&p,2);
printf ("Result values : %ld, %ld, %ld\n", a, b, c);
return 0;
}
Approach-2:
- dos.h header file providing another library function to convert given string into long type.
- The prototype of function is
- long atol(char* s);
#include<stdio.h>
#include<dos.h>
int main()
{
long res;
res = atol("123456");
printf("String \"123456\" converted to long int is %ld\n", res);
return 0;
}