C programming strof, strtod , strtold stdlib.h

In C programming the <stdlib.h> strof , strtod and strtold converts the string literal passed as argument into float,double and long double type.These functions converts string to number.The declaration of the functions are given below.

float strtof(const char * restrict x , char ** restrict endx);
float strtod(const char * restrict x , char ** restrict endx);
float 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-the second part- 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” -the second one is a macros for the infinity value.

  d)”NAN” -it represent the value ‘NaN'(Not a Number).

Code example

char *c ;

printf(“%f”, strtof(“-536.366” , &c) );
printf(“\n%lf”, strtod(“0xa2348c.52” , &c) );
printf(“\n%Lf”, strtold(“2553.35e5” , &c) );

printf(“\n\n%hhi”, *c) ;

Output in VS,



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

Code example

char *c ;

printf(“%f”, strtof( “NAN” , &c ) );

Output in VS,


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

Code example

char *c ;

printf(“%f” , strtof( “INFINITY” , &c ) );
printf(“\n%Lf” , strtold( “-INF” , &c ) );

Output in VS,


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 <float.h>.

Code example

char *c ;

printf(“%i”, FLT_RADIX );
printf(“\n%lf”, strtod( “0Xaa7b8c9” , &c ) );

Output in VS,


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 ;

printf(“%f”, strtof( “ahdhfkdffd” , &c ) << endl
printf(“\n%i” int(*c) ) ;

printf(“\n%lf”, strtod( “” , &c ) );
printf(“\n%i”, 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 or the value losses it’s accuracy after certain digit.In float type after the 7th digit the value loses it’s accuracy.In ‘double’ after the 16th digit the value losses it’s accuracy.In ‘long double’ the 16th digits is rounded to the next greatest digit.

Code example

char *c ;

printf(“%f”, strtof( “7624385543873287464” , &c ) );
printf(“%Lf”, strtold(“21721702836219349839″ , &c” ) );

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


In the first output the digit after the 7th digit the value becomes inaccurate.In the second output the 16 digit which is 4 is rounded to 5.

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 <math.h> header file

Code example

char *c ;

printf(“%f”, HUGE_VAL) ;

/*Passing very large value */
printf(“%f”, strtof( “23.5647e100” , &c ) ) ;

Output in VS,


Related links

->C programming atoi,atol and atoll

->C atof