delorie.com/djgpp/doc/libc/libc_777.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

strtof

Syntax

 
#include <stdlib.h>

float strtof(const char *s, char **endp);

Description

This function converts as many characters of s as look like a floating point number into that number. It also recognises (case-insensitively) "Inf", "Infinity", "NaN", "NaN(optional decimal-number)", "NaN(optional octal-number)" and "NaN(optional hex-number)". If endp is not a null pointer, a pointer to the first unconverted character will be stored in the location pointed to by endp.

Return Value

The value represented by s.

If s is "Inf" or "Infinity", with any variations of case and optionally prefixed with "+" or "-", the return value is INFINITY (if no prefix or a "+" prefix) or -INFINITY (if the prefix is "-").

If s is "NaN" or "NaN()", with any variations of case and optionally prefixed with "+" or "-", the return value is NAN. If the prefix is "-" the sign bit in the NaN will be set to 1.

If s is "NaN(decimal-number)", "NaN(octal-number)" or "NaN(hex-number)", with any variations of case and optionally prefixed with "+" or "-", the return value is a NaN with the mantissa bits set to the lower 23 bits of decimal-number, octal-number or hex-number (the mantissa for floats consists of 23 bits). Use at most 8 hexadecimal digits in hex-number or the internal conversion will overflow, which results in a mantissa with all bits set. If the bit pattern given is 0 (which won't work as a representation of a NaN) NAN will be returned. If the prefix is "-" the sign bit in the NaN will be set to 1. Testing shows that SNaNs might be converted into QNaNs (most significant bit will be set in the mantissa).

If a number represented by s doesn't fit into the range of values representable by the type float, the function returns either -HUGE_VALF (if s begins with the character -) or +HUGE_VALF, and sets errno to ERANGE.

Portability

ANSI/ISO C C99 (see note 1); not C89
POSIX 1003.1-2001; not 1003.2-1992

Notes:

  1. Support for "Inf", "Infinity", "NaN" and "NaN(...)" was standardised in ANSI C99.

Example

 
char buf[] = "123ret";
char buf2[] = "0x123ret";
char buf3[] = "NAN(123)";
char buf4[] = "NAN(0x123)";
char *bp;
float x, x2, x3, x4;

x = strtof(buf, &bp);
x2 = strtof(buf2, &bp);
x3 = strtof(buf3, &bp);
x4 = strtof(buf4, &bp);


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright © 2004     Updated Apr 2004