strtod() function in C/C++
Last Updated :
21 Aug, 2018
The strtod() is a builtin function in C and C++ STL which interprets the contents of the string as a floating point number and return its value as a double.
It sets a pointer to point to the first character after the last valid character of the string, only if there is any, otherwise it sets the pointer to null.
Syntax:
double strtod(str, &end)
Parameters:
str: It specifies the string which has the representation of a floating point number.
end: It is specified the parameter that refers to an already allocated object of type char*.
Return Value: It returns a double value, which is converted from a string, and 0, if no valid conversion can be performed.
Below programs illustrate the above function:
Program 1:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char str[] = "11.03e 0mn" ;
char * end;
double number;
number = strtod (str, &end);
cout << "number = " << str << endl;
cout << "double = " << number << endl;
cout << "end string = " << end << endl;
return 0;
}
|
Output:
number = 11.03e 0mn
double = 11.03
end string = e 0mn
Program 2:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char str[] = "4.06" ;
char * end;
double number;
number = strtod (str, &end);
cout << "number= " << str << endl;
cout << "double= " << number << endl;
if (*end) {
cout << end;
}
else {
cout << "null" ;
}
return 0;
}
|
Output:
number= 4.06
double= 4.06
null
Program 3:
strtod() function with exponents and hexadecimals
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str[] = "-89.04e-3win gfg" ;
char * end;
double number;
number = strtod (str, &end);
cout << "str = " << str << endl;
cout << "double = " << number << endl;
cout << "end string = " << end << endl
<< endl;
strcpy (str, "1998gupta.1204ishwar" );
number = strtod (str, &end);
cout << "str = " << str << endl;
cout << "double = " << number << endl;
cout << "end string = " << end << endl;
return 0;
}
|
Output:
str = -89.04e-3win gfg
double = -0.08904
end string = win gfg
str = 1998gupta.1204ishwar
double = 1998
end string = gupta.1204ishwar
Program 4:
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char * end;
cout << "Infinity"
<< " to double = "
<< strtod ( "infinity" , &end) << endl;
cout << "end string = " << end << endl
<< endl;
cout << "Infpqrs"
<< " to double = " <<
strtod ( "Infpqrs" , &end) << endl;
cout << "end string = " << end << endl
<< endl;
cout << "NaN11x"
<< " to double = "
<< strtod ( "NaN11x" , &end) << endl;
cout << "end string = " << end << endl
<< endl;
return 0;
}
|
Output:
Infinity to double = inf
end string =
Infpqrs to double = inf
end string = pqrs
NaN11x to double = nan
end string = 11x
Program 5:
strtod() function with leading whitespace
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char * end;
cout << "99.99"
<< " to double = "
<< strtod ( " 19.99" , &end) << endl;
cout << "end string = "
<< end << endl
<< endl;
cout << "xyz1.80"
<< " to double = "
<< strtod ( "xyz1.80" , &end) << endl;
cout << "end string = " << end << endl
<< endl;
return 0;
}
|
Output:
99.99 to double = 19.99
end string =
xyz1.80 to double = 0
end string = xyz1.80
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...