strtod() Function
The strtod()
function is declared in the header file <stdlib.h>
.
The strtod()
function converts a string representing a floating-point number into its corresponding double
value. It parses the string, skipping any leading whitespace, and interprets the sequence of characters that form a valid floating-point literal according to the current locale settings.
Syntax of strtod()
double strtod(const char *str, char **endptr);
Parameters
Parameter | Description |
---|---|
str | C-string starting with the representation of a floating-point number. |
endptr | Pointer to a pointer that will be set to the character after the last character used in the conversion. This can be NULL if the result is not needed. |
The function first skips any leading whitespace characters. It then attempts to interpret a floating-point number which may include an optional sign, a series of digits, an optional decimal point, and an optional exponent part (indicated by an ‘e’ or ‘E’ followed by an optional sign and digits).
Return Value
On success, strtod()
returns the converted floating-point number as a double
. If no valid conversion is performed, it returns 0.0. In case the number is out of range, it returns HUGE_VAL
or -HUGE_VAL
and sets errno
to ERANGE
. If underflow occurs, a value not greater than the smallest normalized positive number is returned and errno
is set to ERANGE
.
Examples for strtod()
Example 1: Basic Conversion from String to Double
This example demonstrates how to convert a string containing a valid floating-point number to a double
value using strtod()
.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = " -123.456e2";
char *end;
double num;
num = strtod(str, &end);
printf("Converted number: %f\n", num);
printf("Remaining string: %s\n", end);
return 0;
}
Explanation:
- The string
" -123.456e2"
is provided, which includes leading whitespace. strtod()
skips the whitespace and converts-123.456e2
to adouble
.- The pointer
end
is set to the first character after the numerical value. - The program prints the converted number and the remaining part of the string.
Program Output:
Converted number: -12345.600000
Remaining string:
Example 2: Handling Non-Numeric Trailing Characters
This example shows how strtod()
processes a string that contains valid floating-point data followed by non-numeric characters.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = "3.14159pi";
char *end;
double num;
num = strtod(str, &end);
printf("Converted number: %f\n", num);
printf("Remaining string: %s\n", end);
return 0;
}
Explanation:
- The string
"3.14159pi"
contains a valid floating-point number followed by alphabetic characters. strtod()
converts the numerical part"3.14159"
to adouble
.- The
end
pointer is set to point at the first character after the number, which is'p'
. - The program prints the converted number and the remaining string starting from
'p'
.
Program Output:
Converted number: 3.141590
Remaining string: pi
Example 3: No Valid Conversion and Error Handling
This example demonstrates the behavior when the input string does not contain a valid floating-point number.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = "NoNumberHere";
char *end;
double num;
num = strtod(str, &end);
printf("Converted number: %f\n", num);
printf("Remaining string: %s\n", end);
return 0;
}
Explanation:
- The string
"NoNumberHere"
does not begin with any valid floating-point number. strtod()
performs no conversion and returns 0.0.- The
end
pointer remains pointing at the beginning of the string. - The program prints 0.0 as the converted number and displays the entire string as the remaining part.
Program Output:
Converted number: 0.000000
Remaining string: NoNumberHere
Example 4: Conversion with Underflow or Overflow
This example illustrates how strtod()
handles cases when the converted value is outside the representable range. In such cases, the function returns HUGE_VAL
or -HUGE_VAL
and sets errno
to ERANGE
. (Note: The output may vary depending on the system and the current locale.)
Program
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
int main() {
const char *str = "1e500"; // This value is likely to overflow
char *end;
double num;
errno = 0; // Reset errno before conversion
num = strtod(str, &end);
if (errno == ERANGE) {
printf("Overflow or underflow occurred.\n");
} else {
printf("Converted number: %f\n", num);
}
printf("Remaining string: %s\n", end);
return 0;
}
Explanation:
- The string
"1e500"
represents a number too large to be represented as adouble
. strtod()
attempts the conversion and returnsHUGE_VAL
, whileerrno
is set toERANGE
.- The program checks for
ERANGE
to detect overflow or underflow and prints an appropriate message. - The remaining part of the string is printed, which, in this case, is empty as the whole string was considered part of the number.
Program Output:
Overflow or underflow occurred.
Remaining string: