function
<cstdlib>
atofdouble atof (const char* str);
Convert string to double
Parses the C string str, interpreting its content as a floating point number and returns its value as adouble
.
The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see below), and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.
A valid floating point number for atof using the "C"
locale is formed by an optional sign character (+
or -
), followed by a sequence of digits, optionally containing a decimal-point character (.
), optionally followed by an exponent part (an e
or E
character followed by an optional sign and a sequence of digits).
A valid floating point number for
atofusing the
"C"
locale is formed by an optional sign character (
+
or
-
), followed by one of:
- A sequence of digits, optionally containing a decimal-point character (
.
), optionally followed by an exponent part (an
e
or
E
character followed by an optional sign and a sequence of digits).
- A
0x
or
0X
prefix, then a sequence of hexadecimal digits (as in
isxdigit) optionally containing a period which separates the whole and fractional number parts. Optionally followed by a power of 2 exponent (a
p
or
P
character followed by an optional sign and a sequence of hexadecimal digits).
-
INF
or
INFINITY
(ignoring case).
-
NAN
or
NAN
sequence
(ignoring case), where
sequenceis a sequence of characters, where each character is either an alphanumeric character (as in
isalnum) or the underscore character (
_
).
0.0
.
double
value.
0.0
).
double
, it causes undefined behavior. See strtod for a more robust cross-platform alternative when this is a possibility.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* atof example: sine calculator */
#include <stdio.h> /* printf, fgets */
#include <stdlib.h> /* atof */
#include <math.h> /* sin */
int main ()
{
double n,m;
double pi=3.1415926535;
char buffer[256];
printf ("Enter degrees: ");
fgets (buffer,256,stdin);
n = atof (buffer);
m = sin (n*pi/180);
printf ("The sine of %f degrees is %f\n" , n, m);
return 0;
}
Enter degrees: 45 The sine of 45.000000 degrees is 0.707101
If str does not point to a valid C-string, or if the converted value would be out of the range of values representable by a double
, it causes undefined behavior.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4