C++ ctsdlib strof, strtod and strtold

The C++ <cstdlib> strof , strtod and strtold converts the string literal passed as argument into float,double and long double type.These functions converts string to number.

float strtof(const char * restrict x , char ** restrict endx);
double strtod(const char * restrict x , char ** restrict endx);
long double strtold(const char * restrict x , char ** restrict endx);

x -A string literal.

endx -Stores the last character of the string literal passed,namely ‘0/’ whose integer value is 0.

Return type
floating point -The first function returns float value,the second function returns double value and the third function returns long double.

All the three functions compute the same value,the only difference is the last character appended to the fucntion name.In ‘strtof’ the ‘f’ character stands for ‘float’ and it signify the return type,in ‘strtod’ the ‘d’ stands for ‘double’ which signify the return type of the function and in the third function the ‘ld’ appended to the function name stands for ‘long double’ which signify the return type of the function.

Some points to note:

i)The three functions operate in such way that at first the function decompose the string literal (passed as argument) into three parts :

 a)First part: An empty or a white-space.
 b)Second part: The sequence of characters to be converted into the floating point value.
 C)Third part: The last part consist of some unrecognized characters including the null terminating character.

After this they try to convert the literal into the corresponding floating point value.

ii) The arguments passed can be any of the types given below:

  a) A +ve or -ve sign followed by character sequence which can be converted to decimal or floating point value.

  b)A ‘0X’ or ‘0x’ followed by hexadecimal characters .

  c)”INF” or “INFINITY”.


Code example

char *c ;

cout<< strtof( “-536.366” , &c ) << endl
<< strtod( “0xa2348c.526” ,&c ) << endl
<< strtold( “2553.35e5” , &c ) << endl ;

cout<< int(*c) ;

Output in code::blocks,


iii) If ‘NAN’ is passed ‘quite nan’ is returned if the return type support it,else it is implementation defined.

Code example

char *c ;

cout<< strtof( “NAN” , &c ) ;

Output in code::blocks


iv) If ‘INF’ or ‘INFINITY’ is passed the ‘inf'(infinity) value is returned.

Code example

char *c ;

cout<< strtof( “INFINITY” , &c ) << endl
<< strtold( “-INF” , &c )

Output in code::blocks


v) If the hexadecimal character sequence is passed and ‘FLT_RADIX’ is power of 2 the result is correctly rounded off.You can check the value of ‘FLT_RADIX’ macro using the header <cfloat>.

Code example

char *c ;

cout<< FLT_RADIX << endl ;
cout<< strtod( “0Xaa7b8c9” , &c ) ;

Output in Code::blocks,


vi) If you passed a non-convertible string literal then no conversion is performed and the functions returned 0.The second argument however stors the first character of the string passed.If empty string is passed the second argument stores the value of the first argument.

Code example

char *c ;

cout<< strtof( “ahdhfkdffd” , &c ) << endl
<< int(*c) << endl ;

cout<< strtod( “” , &c ) << endl
<< int(*c) ;

Output in CB,

97 (‘a’ character is equal to 97 in ASCII chart )

vii) If the string passed consist of decimal digits the rounding mode is done in the current rounding direction.This simply means if the 7th digits is greater than 4 then the 6th digit is incremented by 1 ,else the 6th digit remains the same.

Code example

char *c ;

cout<< strtof( “762438724387328790” , &c ) << endl ///the 7th character is greater than 4
<< strtold(“21721702836219329839″ , &c” ) ; ///the 7th character is smaller than 4

Output in CB, (note the 6th value in the output)

7.62439e+017 (6th value incremented by 1)
2.17217e+019 (6th value remains the same)

viii) If the converted value is too large for the return type to represent than HUGE_VAL ,HUGE_VALF , or HUGE_VALL is returned and the value of the macro ERANGE is stored in ‘errno’.Include the header <cmath> to test the value of HUGE_VAL ,HUGE_VALF , or HUGE_VALL.

Link C++ cmath header file

Code example

char *c ;

cout<< HUGE_VAL << endl ;

/*Passing very large value */
cout<< strtof( “23.5647e100” , &c ) ;

Output in CB,


ix) You cannot passed the variable of the ‘string’ data type but you can pass the pointer to char type.It seems the functions cannot converted the argument into floating point value if the type is ‘string’.

Code example

char *c ;

chat st[]=”536″ ;

string str=”637″ ;

/* cout<< strtold( &str , &c ) ; ///error */
/* cout<< strtold( string(“3784.53”) , &c ) ; ///error */

cout<< strtof( st , &c ) ; //work fine



Related links

->C programming strof,strtod,strtold stdlib.h