Menu

#58 printf doesn't accept %f values

v1.0_(example)
open
nobody
None
5
2015-01-27
2015-01-20
apj
No

It seems float values of printf are not returned.
%d works ok.

Discussion

  • Olaf Barthel

    Olaf Barthel - 2015-01-21

    clib2 has separate versions of the printf and scanf functions in libc and libm. The libc versions are shorter because they do not support floating point data, and therefore do not pull in the floating support code.

    If you need floating point support in printf and scanf, then you will need to link with libm before libc is used, e.g. "gcc -o example example.c -lm -lc" rather than just use "gcc -o example example.c".

     
    • apj

      apj - 2015-01-25

      Now I need snprintf. Can you add it's float version to libm, please ?

       

      Last edit: apj 2015-01-25
  • Olaf Barthel

    Olaf Barthel - 2015-01-27

    Are you sure that this is not working as advertized? The whole printf family is part of libm, which includes the snprintf() function that's not actually part of ISO 'C' (1994), but of C99.

    A program such as this ought to work (I tested clib2 version 1.205 for 68k):

    #include <stdio.h>
    
    int
    main(void)
    {
       char buffer[256];
    
       snprintf(buffer,sizeof(buffer),"float = %f",123.456);
    
       printf("%s\n",buffer);
    
       return(0);
    }
    

    I saved this program to disk and compiled it with "gcc -o test test.c -lm -lc". Running it produced the output "float = 123.456000".

     

Log in to post a comment.

MongoDB Logo MongoDB