The C characters and strings: using atof() C function to convert a string to a double value


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Converting the given string to double value using atof() C function

To show: How to use atof() C function to convert a string to double value





// Using atof()/_wtof() - converting string to double

#include <stdio.h>


int main(void)


double dou;

dou = atof("95.0");

printf("Using atof() - converting string to double\n");


printf("The string \"95.0\" when converted to double is %.3f\n", dou);

printf("The converted value, %.3f divided by 2 is %.3f\n", dou, dou / 2.0);


return 0;



Output example:


Using atof() - converting string to double


The string "95.0" when converted to double is 3415728.000

The converted value, 3415728.000 divided by 2 is 1707864.000

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index