strtol
long  strtol ( const char * string, char** endptr, int radix );
stdlib.h
  cplusplus.com  

Convert string to long integer.
  Parses string interpreting its content as an integer value until a character that can not be interpreted is found, and returns a long int value.

Parameters.

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 charsInterpreted by strol as
0xhexadecimal: radix 16
0octal: radix 8
any number from 1 to 9decimal: radix 10

Return Value.
  The converted long int value from the input string.
  If conversion would cause overflow the result is LONG_MAX or LONG_MIN.
  If an error occurs or no conversion can be made 0 is returned.

Portability.
  Defined in ANSI-C.

Example.

/* strtol example */
#include <stdio.h>
#include <stdlib.h>

main ()
{
  char szInput [256];
  char * pEnd;
  long l;
  printf ("Enter an integer value: ");
  gets (szInput);
  l = strtol (szInput,&pEnd,0);
  printf ("Value entered: %ld. Its double: %ld\n",l,l*2);
  return 0;
}

See also.
  atof, strtol, strtoul


© The C++ Resources Network, 2000