Re: sprintf function

From: Martin Ambuhl (mambuhl_at_earthlink.net)
Date: 02/08/04


Date: Sun, 08 Feb 2004 20:52:41 GMT

Earth wrote:
...
> I have written a testing program to try the sprintf fuction and expect
> the output is 1.234. However, the output shows nothing.
...
> #include <stdio.h>
> char* toString(double d)
> {
> char buffer[9];
> sprintf(buffer, "%lf", d);
> return buffer;
> }
>
> main()
> {
> printf("%s\n", toString(1.234));
> }

But this code ...

#include <stdio.h>
char *toString(double d)
{
     static char buffer[9];
     sprintf(buffer, "%lf", d);
     return buffer;
}

int main(void)
{
     printf("%s\n", toString(1.234));
     return 0;
}

Has this output:
1.234000

Notice the difference?

-- 
Martin Ambuhl


Relevant Pages

  • sprintf function
    ... I am new to c and trying the sprintf function. ... I have written a testing program to try the sprintf fuction and expect ... char* toString ...
    (comp.lang.c)
  • Re: m_HttpResponse does not show image using rdbuf()
    ... How on earth do you expect this to work? ... cast filebuf* to char*? ... With sufficient thrust, pigs fly just fine. ...
    (microsoft.public.vc.atl)
  • Re: Accessing void * buffer/array through char * pointer
    ... What on earth is wrong with the 2? ... I think it is better to say "may invoke undefined behaviour". ... implementations and you, as the programmer, can know (with absolute ... one should always used unsigned char for this purpose since ...
    (comp.lang.c)
  • Re: FIRST new bug?!?! =)
    ... > how on earth did you remember to make a 255/256 char long filename? ... > Is that the situation when your program doesn't check if your filename is ... To bad the filer ...
    (comp.sys.hp48)
  • Re: e mail
    ... ocurred in the script on this page.line 1344 char 1.error ... What on EARTH has Office got to do with priniting unavailable in Outlook ...
    (microsoft.public.windowsxp.basics)