The wcstof() functions convert the initial portion of the wide-character string pointed to by str to a float point value. The str parameter points to a sequence of characters that can be interpreted as a numeric floating-point value. These functions stop reading the string at the first character that it cannot recognize as part of a number i.e. if the first character is any of any type except numbers the function terminates there only. This character can be the wchar_t null character at the end of the string.
The standard library functons beginning with ‘wcs’ are declared in wchar.h library in C.
Syntax :
float wcstof (const wchar_t* str, wchar_t** endptr); Parameters : str : C wide string beginning with the representation of a floating-point number. endptr : Reference to an already allocated object of type wchar_t*, whose value is set by the function to the next character in str after the numerical value. This parameter can also be a null pointer. Return value : Floating point value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, range error occurs and is returned.
// C code to convert string having // floating point as its content // using wcstof function #include <stdio.h> // header file containing wcstof function #include <wchar.h> int main() { // wide Character array to be parsed wchar_t str[] = L"58.152 9.26"; // Wide Character array to be parsed wchar_t* pEnd; // float variable to store floating point values float f1, f2; // Parsing the float values to f1 and f2 f1 = wcstof(str, &pEnd;); f2 = wcstof(pEnd, NULL); f2 = 2 * f2; // Printing parsed float values of f1 and f2 wprintf(L"%.2f\n%.2f\n", f1, f2); // operation being performed on the values parsed wprintf(L"Value of Pie is %.2f .\n", f1 / f2); return 0; } |
Output:
58.15 18.52 Value of Pie is 3.14 .
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to print range of basic data types without any library function and constant in C?
- isgraph() C library function
- difftime() C library function
- Unordered Sets in C++ Standard Template Library
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- How to add "graphics.h" C/C++ library to gcc compiler in Linux
- C Library math.h functions
- wprintf() and wscanf in C Library
- snprintf() in C library
- Difference between Header file and Library
- SDL library in C/C++ with examples
- ctype.h(<cctype>) library in C/C++ with Examples
- How to call function within function in C or C++
- Write a one line C function to round floating point numbers
- Does C support function overloading?
- How can I return multiple values from a function?
- What is the purpose of a function prototype?
- How to declare a pointer to a function?
- C function to Swap strings
- Can we call an undeclared function in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

