C Programming

Atof() Function in C Language

The conversion functions from one data type to another are very useful in several cases. They allow us to use the different functions with different data types in their input and output arguments to process them.

Generally, the data that the user enters into the system through a command console, a graphical user interface, or a text file is created in the form of strings which we later need to convert into integers, doubles, floats, etc. in order to process them.

In this Linux Hint article, we will show you how to use the atof() function to convert the strings to their numeric value of float point or double type.

We will take a brief look at the theory of this function, the input and output arguments, and the data types it accepts. Then, using practical examples with code fragments and images, we will implement the use of the atof() function in various cases.

Atof() Function Syntax in C Language

double atof ( const char *str );

Atof() Function Description in C Language

For the following expression, the atof() function converts the “str” string from its input argument and returns its numeric value of float point in the output double, “a”.

a = atof ( * str );

The conversion starts with the most significant character of the value and ends with the least significant character of the value, ignores the spaces, and ends when a non-numeric character occurs. In cases where conversion is not possible due to multiple cases, like if the string which is sent in the input argument does not contain the numeric characters, atof() returns 0 as the result in “a”.

These conversion functions are among those which are included in the standard library. We have to include the “stdlib.h” header in our “.c” or “.h” files by the following line before using them:

#include <stdlib.h>

Once the “stdlib.h” header is included, we can use atof() and the functions it contains.

Next, we will see some practical examples where we implement this function.

How to Convert a String to Numeric Value Using the Atof() Function in the C Language

In this example, we will see how to use the atof() function to convert a string to a numeric value. To do this, we create the “str” string with the string that represents the square root of 2 and the “a” variable where we store the numeric result of the conversion.

To convert the string to its numeric value, we call the atof() function and pass the str string as the input argument.

The following code snippet shows the correct conversion of a string to a numeric value. The converted value is stored in the double “a”:

#include <stdio.h>

#include <stdlib.h>

void main ()
  {
 char str [20] = "1.4142";
double a;
   a = atof ( str );
  }

In this way, with the atof() function, we have the value entered as a string in floating point format in a double type to be able to process it with the various mathematical functions that we have in the C language.

Once we have our code ready, we compile it and run it in the Linux console with the following command line:

~$ gcc Documents/atof_ex1.c -o atof_1

~$ ./atof_1

The following image shows the compilation for this code:

The Atof() Function with Non-Numeric Characters in Its Input String

The atof() function only accepts the numeric characters and a decimal point for conversion. In cases where none of these characters are present in your input string or only a decimal point is found without any numeric characters, atof() returns 0, as shown in the following example:

#include <stdio.h>

#include <stdlib.h>

void main ()
  {
 char str [20] = "Hello World";
double a;
   a = atof ( str );
printf( "a = %f", a);
  }

The following figure shows the results for an input string which contains the alphabetic or non-numeric characters:

White Space Within the Input String of the Atof() Function in C Language

When this function is called, atof() ignores the spaces it finds before the first numeric character of the string. When it finds a numeric character, it starts converting from the most significant character to the least significant character. The conversion stops and the function returns when it finds a non-numeric character, be it a letter or a white space.

#include <stdio.h>

#include <stdlib.h>

void main ()
  {
 char str [20] = "            12345";
double a;
   a = atof ( str );
printf( "a = %f", a);
  }

The following image shows how whitespace is ignored by atof():

Conclusion

In this Linux Hint article, we showed how to use the atof() function which is one of the functions of the standard library of the C language to convert the variables from one data type to another. We reviewed the theoretical part of this function and the types of data accepted in its inputs and outputs. We also showed you, with working examples, on how to implement this function and its behavior with different characters in your input string. We hope that this article is useful to you. For more articles and tips like this, use the search engine on our Linux Hint website.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).