strtol function


strtol will convert a string to a long integer. An important feature of this function is the ability to accept data in various number bases and convert to decimal. If you are just working with decimal numbers, atoi is probably an easer function to use.



Library:   stdlib.h

Prototype: long int strtol(const char *sptr, char **endptr, int base);

Syntax:	   char     String[]="ff";		/* string to convert	*/
	   int      Base=16;			/* Base 16		*/
           long int Ans;			/* Result		*/

           Ans = strtol(String, NULL, Base);


Notes

string
String representing an integer number. The number is considered until a non-numeric character is found (only the digits from 0 to radix-1 are considered valid numeric characters, signs are considered valid numeric characters for this parameter). The format used is:
[whitespaces][+|-][0|0x][nnnnn]
(where whitespaces are any tab or space character and nnnnn is a sequence of valid numbers following the specified radix)
endptr
Address of a pointer. This is filled by the function with the address where scan has ended. Serves to determine where there is the first non-numerical character in the string.
radix
Numeral radix in which the number to be interpreted. Must be 0 or be between 2 and 36. If it is 0 the radix of the string is determined by the initial characters of the string:
initial chars Interpreted by strol as
0x hexadecimal: radix 16
0 octal: radix 8
any number from 1 to 9 decimal: radix 10

The Third argument (base) can have a value of 0 or 2-32.


Examples:

Example program.


See also:

atoi String to integer conversion.

atof String to floating point conversion.

atol String to long integer conversion.

strtod String to double conversion.

strtoul String to unsigned long integer conversion.

Conversion table.


Top Master Index Keywords Functions


Martin Leslie