Atof () function in C

The double atof function (const char * str) in Standard C Library converts a string pointed to by parameter str to a floating point number (type double).

The double atof function (const char * str) in Standard C Library converts a string pointed to by parameter str to a floating point number (type double).

Declare atof () function in C

Below is the declaration for the atof () function in C:

 double atof ( const char * str ) 

Parameters

str - This is a string with a representation of a floating point number.

Returns the value

This function returns the floating point number that was converted as a double value. If no valid conversion is performed, it returns 0.0.

For example

The following C program illustrates the usage of the atof () function in C:

 #include #include #include int main () { float val ; char str [ 20 ]; strcpy ( str , "98993489" ); val = atof ( str ); printf ( "Gia tri duoi dang chuoi = %s, nGia tri duoi dang so thuc = %fn" , str , val ); strcpy ( str , "QTM.com" ); val = atof ( str ); printf ( "nGia tri duoi dang chuoi = %s, nGia tri duoi dang so thuc = %fn" , str , val ); return ( 0 ); } 

Compile and run the above C program to see the results.

According to Tutorialspoint

Last lesson: stdlib.h in C

Next lesson: The function atoi () in C

5 ★ | 1 Vote