How to use sprintf() with float

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello!

I can't solve the problem with using float in family of printf() functions in my C project.

I use EZ-USB SDK 1.3 with SUPERSPEED EXPLORER KIT.

I want to use sprintf() and vsprintf() (with va_list) for creating debug messages.

But when i add printf() family functions into my project, I receive linker error (picture 1):

1_page.png

Picture 1

I read in different forums, that for using float int printf() there are two ways:

1. Add to my project cyfxcppsyscall.cpp file (with _sbrk definition) and #include <sys/unistd.h> file, where using sprintf().

Also add __heap_start and __heap_end definition in fx3.ld linker file.

It's not work - i also see undefined reference to '_sbrk'

2. Add linker flags: -specs=nosys.specs -specs=nano.specs -u _printf_float

See page:

2_page.png

In second

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Alexdobrikov-san,

I've just skimmed "cyfxcppsyscall.cpp", it seems to be a "c" source file,

but  the extension ".cpp" may suggest c++.

So in case c++ (or g++) is called to compile this source file

the symbols will be quite different from what "c" is expecting.

I'd suggest you to try following (if you don't mind)

if any of these solves the problem.

(1) remove this source file from your project

(2) rename this source file to cyfxcppsyscall.c

(3) add "extern "C" { " and "}" at  the beginning and the end of this source file.

moto

View solution in original post

13 Replies
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

May be this is not your expecting answer, but I usually do something like

====================

float f_value ;

char sign = ' ' ; /* a space */

...

if (f_value < 0) {

    sign = '-' ; /* minus */

    f_value = -1.0 * f_value ;

}

sprintf(str, "%c%d.%02d", sign, (int)f_value, ((int)(100 * f_value + 0.5))%100) ;

====================

moto

0 Likes
Anonymous
Not applicable

Hi, thank you for answer.

I want to use printf() family in situation something like this:

void UART_printf(char *arg_list, ...)

{

        va_list arg_buffer;

        va_start(arg_buffer, arg_list);

        memset(uart_printf_buf, 0, sizeof(uart_printf_buf));

        vsprintf(uart_printf_buf, arg_list, arg_buffer);

        va_end(arg_buffer);

}*/

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi, Alexdobrikov-san,

So, in your context, does this work?

====================

float f_value = 0.5 ;

char sign = ' ' ; /* you may not need this, if you don't get negative value */

if (f_value < 0.0) {

     f_value = -1.0 * f_value ;

    sign = '-' ;

}

counter = sprintf(&t->buffer[t->in], "%c%d.%02d",(int)f_value, ((int)(100 * f_value + 0.5) % 100) ;

====================

And with this method, you don't have to modify the compiler flags from the default, I hope.

moto

0 Likes
Anonymous
Not applicable

Problem in sprintf():

5_page.png

0 Likes
Anonymous
Not applicable

if i comment sprintf(), problem with _sbrk() get out.

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Alexdobrikov-san,

Can you at least remove -u _printf_float flag? others also may count...

Then could you try Clean and Build the project?

moto

0 Likes
Anonymous
Not applicable

I restore linker flags...

6_page.png

But have the same problem with undefined _sbrk()

7_page.png

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Alexdobrikov-san,

I've just skimmed "cyfxcppsyscall.cpp", it seems to be a "c" source file,

but  the extension ".cpp" may suggest c++.

So in case c++ (or g++) is called to compile this source file

the symbols will be quite different from what "c" is expecting.

I'd suggest you to try following (if you don't mind)

if any of these solves the problem.

(1) remove this source file from your project

(2) rename this source file to cyfxcppsyscall.c

(3) add "extern "C" { " and "}" at  the beginning and the end of this source file.

moto

MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Alexdobrikov-san,

In case, none of trials in my previous message worked,

could you restore your project to the condition before you tried to add float printf?

Then add "#include <stdio.h>" and my method, which does not require any float

printf/sprintf operation and try if it works for you?

Best Regards,

16-Nov-2018

Motoo Tanaka

0 Likes
Anonymous
Not applicable

Thank you Mr. Motoo Tanaka!

0 Likes
Anonymous
Not applicable

I'd suggest you to try following (if you don't mind)

if any of these solves the problem.

(1) remove this source file from your project

(2) rename this source file to cyfxcppsyscall.c

(3) add "extern "C" { " and "}" at  the beginning and the end of this source file.

It works!!!)

My mistake was that i add in project cyfxcppsyscall.cpp file , but must add .c file.

MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Alexdobrikov-san,

I think that now it's not the problem of the source code,

but the compiler flags you modified.

By any chance, can you restore the compiler flags before you modified them?

moto

0 Likes
Anonymous
Not applicable

some problems with my pale-moon...

In second way if i add linker flags, i see picture 3:

3_page.png

Picture 3

Error1 make

4_page.png

And i don't understand what i must do with this error?

0 Likes