atof() function in C
The name of the function itself will represent its working principle, which is converting the ASCII values into floating-point numbers. The atof() function is used to convert the string into float values. In this blog, we will learn about the atof() function with its syntax and examples.
Syntax:
It has the fo
llowing syntax:
double atof(const char *str)
Here, str is the string that represents the floating-point number.
A program to convert the ASCII values into floating point numbers without using the inbuilt function
Program:
#include <stdio.h>
#include <math.h>
float convert_string_to_float(const char *orginal_string) {
float result = 0; // number from orginal_stringing
int check_sign= 1; // checking whether the orginal_stringing is float or integer
int i = 0; // Index
int check_decimals=0;
int decimals=1;
// Check for negative sign
if (orginal_string[0] == '-') {
check_sign= -1;
i = 1;
}
for (; orginal_string[i] != '\0'; ++i) {
if(orginal_string[i]!='.'){
// if the character is '.' which represents the decimals
if(check_decimals==1){
decimals = decimals + 1;
}
int digit = orginal_string[i] - '0'; // getting the digit from character
// Check if the character is a valid digit or not
if (digit >= 0 && digit <= 9) {
result = result * 10 + digit;
}
}else if(orginal_string[i]=='.' &&check_decimals==0){
//If the character is '.' then we count decimal points
check_decimals=1;
}else{
//If other symbols are countered, we make them stop converting
break;
}
}
printf("%d",decimals);
return (result * check_sign)/pow(10,decimals-1) ; //we multiply check_signto the result
}
int main() {
const char *orginal_string = "123.45";
// by using the normal method without using the inbuilt functions
float value = convert_string_to_float(orginal_string);
printf("orginal_string: %s\n", orginal_string);
printf("float value using the normal method: %f\n", value);
return 0;
}
Output:
3orginal_string: 123.45
float value using the normal method: 123.449997
Using the standard inbuilt function
Parameters:
- We can give the string to the function in the form of a group of characters.
- We can give a null-terminated string representing the floating point number we want to convert.
Example:
const char *str = "123.45";
- We can give an array of elements containing digits in the form of characters.
Example:
char str2[] = "678.90";
char str3[] = {'8', '9', '1', '2', '.', '7', '9', '\0'};
Conversion process behind this function:
The atof() function looks at the input string, a sequence of characters representing a number. It starts reading the characters in the string, ignoring any empty spaces at the beginning. It looks for the first significant character, a digit (0-9), or a sign (+ or -) indicating if the number is positive or negative. After that, it reads the digits and builds the whole number part of the floating-point number. For example, if the string is "123.45", it reads "123" as the whole number part.
If there is a decimal point (.), the atof() function recognizes it and starts reading the fractional part of the number. For example, "123.45" reads ".45" as the fractional part.
It continues reading digits after the decimal point until it reaches a non-digit character or the end of the string. For instance, the string "123.45abc" stops reading at the character 'a' because it is not a digit.
If the string contains an exponent part, which is a way to represent very large or very small numbers using scientific notation (e.g., "1.23e-4" means 1.23 times 10 to the power of -4), the atof() function recognizes it, and adjusts the value accordingly. It handles the exponent part by multiplying or dividing the number by the appropriate power of 10.
Finally, the atof() function returns the resulting double value, representing the number in the input string.
In summary, the atof() function reads the string character by character, constructs the whole number and fractional parts, and handles special cases like signs and exponents. After that, it calculates and returns the corresponding floating-point value.
A program to convert the string to float in c language
#include <stdio.h>
#include <stdlib.h>
int main() {
// using the atof function to convert string to float
const char *str = "123.45";
double value = atof(str);
printf("String: %s\n", str);
printf("Double value: %f\n", value);
char str2[] = "678.90";
double value2 = atof(str2);
printf("String: %s\n", str2);
printf("Double value: %f\n", value2);
char str3[] = {'8', '9', '1', '2', '.', '7', '9', '\0'};
double value3 = atof(str3);
printf("String: %s\n", str3);
printf("Double value: %f\n", value3);
return 0;
}
Output:
![atof() function in C](https://static.tutorialandexample.com/c/atof-function-in-c1.png)
Conclusion:
In simple terms, atof() converts a string to a floating-point number by understanding the different parts of the number, like the sign, whole number, fractional part, and exponent. After that, it combines these parts to create the corresponding double value.