C Reference function strtod()

This function of stdlib will convert a string to a double.

Usage of strtod():

double strtod ( const char * str, char ** endptr );


C string str interpreting its content as a floating point number.
If endptr is not a null pointer, the function also sets the value pointed by endptr to point to the first character after the number. White-spaces are discarded until the first non-whitespace character is found.
A valid floating point number for atof is formed by: plus or minus sign, sequence of digits, decimal point and
optional exponent part (character ‘e’ or ‘E’)

Return value:

The function returns the converted floating point number as a double value, if successful.
If no valid conversion could be performed then an zero value is returned.

Source code example of strtod():


	int main ()
		char input[] = "10.0 5.0";
		char * ptr_end;
		double a, b;

		a = strtod (input,&ptr_end);
		b = strtod (ptr_end,NULL);

		printf ("Output= %.2lf\n", a/b);

		return 0;

Output of the strtod example program above:

	Output= 2.0

This entry was posted in C Reference stdlib.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.