beautypg.com

Strtod, strtol, strtoul functions, Strtod, strtol, strtoul – Zilog Z80380 User Manual

Page 86

background image

Library Functions

Run Time Environment

4–22

UM004001-COR1103

Return Value

The first time strtok is called, it returns a pointer to the first token in string1. In later calls
with the same token string, strtok returns a pointer to the next token in the string. A NULL
pointer is returned when there are no more tokens. All tokens are null-terminated.

strtod, strtol, strtoul

FUNCTIONS

Header file statement:

#include

Syntax:

double strtod

( const char *nptr, char **endptr );

long strtol

( const char *nptr, char **endptr, int base );

unsigned long strtoul

( const char *nptr, char **endptr, int base )

The strtod, strtol, and strtoul functions convert a character string to a double-precision value,
a long-integer value, or an unsigned long-integer value, respectively. The input string is a
sequence of characters that can be interpreted as a numerical value of the specified type.

These functions stop reading the string at the first character they cannot recognize as part of a
number. This may be the null character (‘\0’) at the end of the string. With strtol or strtoul,
this terminating character can also be the first numeric character greater than or equal to base.
If endptr is not NULL, a pointer to the character that stopped the scan is stored at the location
pointed to by endptr. If no conversion could be performed (no valid digits were found or an
invalid base was specified), the value of nptr is stored at the location pointed to by endptr.

The strtod function expects nptr to point to a string with the following form:

[whitespace] [sign] [digits] [.digits] [ {d | D | e | E}[sign]digits]

A whitespace consists of space and tab characters, which are ignored; sign is either plus (+)
or minus (-); and digits are one or more decimal digits. If no digits appear before the decimal
point, at least one must appear after the decimal point. The decimal digits can be followed by
an exponent, which consists of an introductory letter (b, D, e, or E) and an optionally signed
decimal integer.

The first character that does not fit this form stops the scan.

The strtol and strtoul functions expect nptr to point to a string with the following form:

Parameter

Description

nptr

String to convert

endptr

Pointer to character that stops scan

base

Number base to use