[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


strtol

Character array conversion to an integral value of type long int.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


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

Parameters for this facility are:

nptr  
const char *  
A Null terminated array to convert  
endptr  
char **  
A pointer to a position in nptry that is not convertible.  
base  
int  
A numeric base between 2 and 36  

Remarks:

The strtol() function converts a character array, pointed to by nptr, expected to represent an integer expressed in radix base, to an integer value of type long int. A plus or minus sign (+ or -) prefixing the number string is optional.

The base argument in strtol() specifies the base used for conversion. It must have a value between 2 and 36, or 0. If base is 0, then strtol() converts the character array based on its format. Character arrays beginning with'0' are assumed to be octal, number strings beginning with'0x' or'0X' are assumed to be hexadecimal. All other number strings are assumed to be decimal.

If the endptr argument is not a null pointer, it is assigned a pointer to a position within the character array pointed to by nptr. This position marks the first character that is not convertible to a long int value.

This function skips leading white space.

This function sets the global variable errno to ERANGE if there is a conversion error.

Return:

strtol() returns an integer value of type long int. If the converted value is less than LONG_MIN, strtol() returns LONG_MIN and sets errno to ERANGE. If the converted value is greater than LONG_MAX, strtol() returns LONG_MAX and sets errno to ERANGE. The LONG_MIN and LONG_MAX macros are defined in limits.h.

See Also:

"strtod"

"strtoul"

"errno"

"Integral type limits"

"Overview of math.h"

"scanf"

Example of strtol(), strtoul(), strtoll(), and strtoull() usage:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
	long int i;
	unsigned long int j;
	long long int lli;
	unsigned long long ull;
	static char si[] = "4733 777";
	static char sb[] = "0x10*****";
	static char sc[] = "66E00M???";
	static char sd[] = "Q0N50M abcd";

	char *endptr;
	i = strtol(si, &endptr, 10);
	printf("%ld remainder of string = |%s|\n", i, endptr);
	i = strtol(si, &endptr, 8);
	printf("%ld remainder of string = |%s|\n", i, endptr);
	j = strtoul(sb, &endptr, 0);
	printf("%lu remainder of string = |%s|\n", j, endptr);
	j = strtoul(sb, &endptr, 16);
	printf("%lu remainder of string = |%s|\n", j, endptr);
	lli = strtoll(sc, &endptr, 36);
	printf("%lld remainder of string = |%s|\n", lli, endptr);
	ull = strtoull(sd, &endptr, 27);
	printf("%llu remainder of string = |%s|\n", ull, endptr);
	return 0;
}

Output:
4733 remainder of string = | 777|
2523 remainder of string = | 777|
16 remainder of string = |*****|
16 remainder of string = |*****|
373527958 remainder of string = |???|
373527958 remainder of string = | abcd|


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000