C Reference function atof() convert a string to a double

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

Usage of atof():

double atof ( const char * str );

Parameters:

C string str interpreting its content as a floating point 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 atof():


	#include<stdio.h>
	#include<stdlib.h>

	int main ()
	{
		double a,b;
		char buffer [256];
		printf ( "Input: " );
		gets (buffer);
		a = atof (buffer);
		b = a/2;
		printf ( "a= %f and b= %f\n" , a, b );
		return 0;
	}

Output of the atof example program above:


	Input: 12
	a= 12.000000 and b= 6.000000

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.

There is currently one response to “C Reference function atof() convert a string to a double”

Why not let us know what you think by adding your own comment!

  1. patadiya dharmesh on February 16th, 2012:

    Can you give some more examples? This has given me some idea but still has not cleared logic.