- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - atof() function
The C stdlib library atof() function is used to convert a string into a floating-point number and represent the converted floating point number to its corresponding double value.
A floating-point number is a type of integer that includes a fractional part, represented using a decimal point. For example, numbers like 10.05 and 5.5005 are floating-point numbers.
Syntax
Following is the C library syntax of the atof() function −
double atof(const char *str)
Parameters
This function accepts a single parameter −
-
str − It is a pointer to a null-terminated sting, which represent the a floating point number.
Return Value
This function returns a floating-point number that corresponds to its double representation. If the input string is not a valid floating-point number, it returns 0.0.
Example 1
The following is the basic c example that demonstrate the use of atof() function.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
double res;
char *str;
str = "-1509.10E-10";
res = atof(str);
printf("res = %.4e\n", res);
}
Output
Following is the output −
res = -1.5091e-007
Example 2
In this example, we concatenated two string and then convert the resulting string into a floating point number using the atof() function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Define two strings to concatenate
char str1[] = "123.456";
char str2[] = "789";
//calculate the length of string first + second
int length = strlen(str1) + strlen(str2) + 1;
// Allocate memory for the concatenated string
char *concatenated = malloc(length);
// check memory allocation if null return 1.
if(concatenated == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Concatenate str1 and str2
strcpy(concatenated, str1);
strcat(concatenated, str2);
// Convert concatenated string into a floating point number.
// use the atof() function
double number = atof(concatenated);
printf("The concatenated string is: %s\n", concatenated);
printf("The floating point number is: %f\n", number);
// at the last free the alocated memory
free(concatenated);
return 0;
}
Output
Following is the output −
The concatenated string is: 123.456789 The floating point number is: 123.456789
Example 3
The following is the another example, here we convert the both numerical string and character string into the floating-point number.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
float res;
char str[20];
//define a string
strcpy(str, "151413.10e");
//convert into float
//use atof() function
res = atof(str);
printf("String value = %s\n" , str);
printf("Float value = %f\n", res);
strcpy(str, "tutorialspoint.com");
//use atof() function
res = atof(str);
printf("String value = %s\n", str);
printf("Float value = %f\n", res);
return(0);
}
Output
Following is the output −
String value = 151413.10e Float value = 151413.093750 String value = tutorialspoint.com Float value = 0.000000