Convert a String into Long Value

What string function is used to convert a string into long value?

This is done by using the function named as atol() . This function atol() is present in the header file named as <stdlib.h>. When programmers use this function they must include the header file by the statement

    #include <stdlib.h> 

The syntax for this function is given by

    long atol( const char *s)

Here the function atol() converts the string pointed by s into long and the return data type is therefore long. The function atol() starts with string which may have numeric and non numeric value and the function atol gets terminated when it encounters a non numeric character in the string.  To get output from this function it must have at least a numeric character starting in the string.

Let us consider the above function by means of an example:

main()
{
long a;
a= atol(“317.890”);
}

In the  above program the function atol()converts starts with numeric character 3 and proceeds till 317 and terminates on encountering the non numeric character "." and therefore a gets long value as 317L.

Editorial Team at Geekinterview is a team of HR and Career Advice members led by Chandra Vennapoosa.

Editorial Team – who has written posts on Online Learning.


Pin It

Leave a Reply