Long double to string -- 
Author Message
 Long double to string --

The following program, written to convert a long double to string is not
working properly. The output is some junk. Any suggestions??

#include <stdio.h>
#define LEN 16
main()
 {
    char number[LEN];
    long double d = 983.7814891264;
    bzero(number, LEN);
    sprintf(number, "%lf", d);
    printf( " Number is %s", number);

Quote:
}

Thank you very much,
Sheshagiri.


Sat, 22 Nov 2003 23:15:18 GMT  
 Long double to string --

Quote:

> #include <stdio.h>
> #define LEN 16

You sure that's big enough?

Quote:
> main()

C99 prohibits implicit int.

Quote:
>  {
>     char number[LEN];
>     long double d = 983.7814891264;
>     bzero(number, LEN);

bzero() is not a standard function and it is not necessary to
zero out a buffer before passing it to sprintf().

Quote:
>     sprintf(number, "%lf", d);

%Lf is the proper specification for a long double.

Quote:
>     printf( " Number is %s", number);

ISO C requires stdout to be \n terminated.

You should return a value from main().

Quote:
> }

--
Just another C hacker.


Sat, 22 Nov 2003 23:18:40 GMT  
 Long double to string --
Sheshagiri a crit dans le message ...
Quote:
>The following program, written to convert a long double to string is
not
>working properly. The output is some junk. Any suggestions??

>#include <stdio.h>
>#define LEN 16
>main()
> {
>    char number[LEN];
>    long double d = 983.7814891264;
>    bzero(number, LEN);
>    sprintf(number, "%lf", d);
>    printf( " Number is %s", number);
>}

Huh! Try that:

#include <stdio.h>
#include <assert.h>

#define LEN 64

int main (void)
{
   char number[LEN];
   long double d = 983.7814891264;
   {
      int n = sprintf (number, "%Lf\n", d);
      assert (n < LEN);
   }
   printf (" Number is %s", number);
   return 0;

Quote:
}

 Number is 983.781489

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkumware.com/htm_cl/index.html
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Sun, 23 Nov 2003 01:54:47 GMT  
 Long double to string --

Quote:

> The following program, written to convert a long double to string is not
> working properly. The output is some junk. Any suggestions??

#include <stdio.h>
#define LEN 16

 /* mha - you need to at least (1) return an int from main for C89 or
    (2) explicitly declare main to return an int for C99. Both changes
    are made below */

int main()
{
    char number[LEN];
    long double d = 983.7814891264L;    /* mha - If you really want a
                                           long double, use the 'L' to
                                           mark it such.  I have added
                                           this. */
#if 0
    /* mha - bzero is not a standard function.  If you want to zero
       memory, memset or explicit assignment is the way to do it.  It
       is not needed in any case. */
    bzero(number, LEN);
#endif
    sprintf(number, "%Lf", d);  /* mha - the specifier for a long
                                   double is %Lf, not %lf.  I have
                                   fixed this */
    printf(" Number is %s\n", number);  /* mha - If you want portably
                                           defined behavior, you must
                                           terminate the last line of
                                           output with an end-of-line
                                           character.  I have made this
                                           change. */
    return 0;

Quote:
}

 Number is 983.781489
Quote:

> #include <stdio.h>
> #define LEN 16
> main()
>  {
>     char number[LEN];
>     long double d = 983.7814891264;
>     bzero(number, LEN);
>     sprintf(number, "%lf", d);
>     printf( " Number is %s", number);
> }

> Thank you very much,
> Sheshagiri.



Fri, 28 Nov 2003 22:55:04 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. long double to string & string to long double

2. Long long and long double

3. Need function to convert float/double/long to string using a specified format

4. convert string to long double - problem

5. Double to string: _fcvt no longer works ???!!!

6. convert string to long, and long to string

7. can long double be less precise than double?

8. long long integer and double precision number

9. addition long values to long double value ?

10. epsilon for float, double long double

11. Difference between double and long double?

12. Converting long double to double

 

 
Powered by phpBB® Forum Software