C++

The atof() function in C++

C++ is a very vast language for performing different functions and converting a mutable of one type to another. One of those functions is the “atof” function. The C++ “atof” function is used to convert a string type mutable into floating-point value and display or return a double value. Therefore, we will be discussing the C++ atof function in this tutorial article today. Let’s get started with the opening of Ubuntu 20.04 and log in from it. The first step to start the implementation is to open the built-in terminal of the Ubuntu 20.04 system. This can be done with the “Ctrl+Alt+T” shortcut key. Now, the terminal is launched on your Ubuntu 20.04 screen and ready to be used. You need to have some files saved on it to utilize the code. That file will be executed after the compilation. Thus, we have created a new C++ file named “atof.cc” in the terminal with a unique “touch” command, as shown in the attached photo:

We will be working within some text or Nano editor to add our code to it. Thus, we preferred to use the GNU NANO 4.8 editor in our Ubuntu 20.04 to write the code. The command for opening the newly created C++ file has been demonstrated in the attached screenshot:

Example 01:

Finally, we are ready to start our first example code. C++ code can never function without its header files. Thus, we have added two basic and necessary headers, i.e., “iostream” and “cstdlib”. Then, we have used the everlasting “standard” namespace in our code to use the standard syntax of C++ and take input-output. The main() function has been defined to start the code execution. It contains a character type “Arr” variable string having a negative string value. As the inverted commas represent the string, this means all of its characters will be saved to the different indexes of the variable “Arr”.

At the next line, we have declared another variable of a double type named “Arrd”. The “atof()” function has been applied to the character type variable “Arr” to take it as a double value and save it to the double type variable “Arrd” by assignment. Then, we have utilized two cout statements to verily display the values of the original character type string value, i.e., “Arr” and the double value saves in the variable “Arrd”, of the same string. The main function closes here, and we are ready to run our code in the shell. Before that, we have to secure our code in the file with Ctrl+S. One can come back to the terminal screen via the smart Ctrl+X shortcut keystroke:

Make sure to have a compiler of C++ language already configured and built into your system. If not, try to make one with an apt package. We are utilizing the g++ compiler for C++ code. The instruction to compile the code just needs the name of a file, as shown in the picture. After the compilation becomes efficient, we will be running it with the “./a.out” standard execution command of the Ubuntu 20.04 terminal. As an output, it returns the original string value of variable “Arr” as it is. While the other value it returns is a converted double value of a variable “Arrd”, which was first converted into a floating-point via the “atof()” function. You can see the “0” at the end of the string value has been erased in the double value in the output:

Example 02:

Let’s take another example to convert a string type value containing many characters or numbers in it. We have been using the “bits/stdc++.h” header file in our code to avoid any inconvenience during the compilation and execution of code. After adding all the header libraries and “standard” namespace, we have initialized a main() method. The function contains a character type string variable “A” with a value of “Pi” in it. Another variable of a double type named “Ad” has been initialized with a value that has been generated from the atof() function applied on the variable “A”. This would be a double-type floating value.

Two cout statements have been used to display the value of variable “A” as a string and the value of variable “Ad”, i.e., double type floating value for the same “Pi”. Another variable, “val” has been initialized with a string-type numerical value in it. This value has been converted to floating-point with the help of an “atof()” function and saved to the double type variable “vald”. The cout statements have been used to display the original string and converted double type value on the shell for both variables “val” and “vald”. Now the program ends, save it using the old shortcut key “Ctrl+S”:

It’s time to come back to the shell using “Ctrl+X” and compile the jus updated code first. So, we have done that and compiled the updated C++ file with the installed “G++” C++ compiler. The compilation of newly updated code has been quite successful. Then, run the code file with the standard “./a.out” instruction in the shell. The first two lines show the output of variable “A” and its converted double value, i.e., variable “Ad”. The next consecutive two-line output displays variable “val” and its floating-point converted value, i.e., “vald”:

Example 03:

Let’s get to the last example of this article. We will discuss how an “atof()” function works on the nan, infinity, exponents, and hexadecimal values. The three standard libraries, i.e., iostream, cstdlib, and bits/stdc++.h, have been included at the start of this code, and the “standard” namespace is a must, as you know. The main function() has been started after all the prerequisites of this code.

Within the main() function, we have only used the cout statements to directly display the result of the “atof()” function on some values. The first two cout statements have the floating-point double value with an exponent part, i.e., “e”. The very next two cout statements have the atof() function on the hexadecimal. The 5th and 6th cout statements are using atof() on infinity or inf that are used to ignore case. The 7th and 8th cout statements are using atof() on nan, NAN, which is similar to inf and INFINITY and can use a sequence of alphanumeric values in it as well. All the cout statements onward 9th line are trailing and mixed. Let’s see how it works on the shell:

The compilation and execution are completed, and the output is shown below. The first 4 lines show the simple conversion of exponents and hexadecimal to floating-point values using atof(). Lines 4 to 8 shows the converted double value for inf, INFINITY, nan, and NAN using atof(). The “atof” function works well at all the tailing values, onward the 9th line to the end. Only the 10th and 15th lines show 0, i.e., invalid conversion syntax of a value.

Conclusion:

This article has come up with enough examples to illustrate the functioning of the atof() method of C++ language. We have discussed the atof() function working on simple string values, nan, infinity, exponents, and hexadecimal type values without examples. Therefore, we are sure that this article will provide you with the best of it. Check out Linux Hint for more informative articles.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.