cancel
Showing results for 
Search instead for 
Did you mean: 

Wrong result when printing a int64_t value using STM32CubeIDE

BTrem.1
Senior II

I trying to use sprintf to display some values for debug purposes. I am getting wrong results in STM32CubeIDE v1.5.0 but it is working with an online gcc compiler.

The code is in a function and for now I'm re-assigning the values inside the function for debug. Here is my code in STM32CubeIDE:

void log_data(int16_t u, int16_t y, int64_t z, int16_t indx )
{
	u = 50;
	y= 100;
	z= 4351234;
	indx= 0;
 
	static char msg[48];
	sprintf(msg, "%d %d %lld %d \r\n>", u, y, z, indx);
 
	HAL_UART_Transmit_IT(&huart3, (uint8_t *) msg, strlen(msg)); // &huart3 &hlpuart1
}

The result displays:

50 100 ld 4351234

>

In the online gcc compiler I run this program:

// Online C compiler to run C program online
#include <stdio.h>
 
#define int16_t __int16_t
#define int64_t __int64_t
int main() {
    // Write C code here
    int16_t u = 50;
    int16_t y = 100;
    int64_t z = 4351234;
    int16_t indx = 0;
    
    static char msg[48];
    sprintf(msg, "%d %d %lld %d \r\n>", u, y, z, indx);
    printf("Hello world %s" , msg);
    
    return 0;
}

The display is: Hello world 50 100 4351234 0 

All is as I expect it to be.

Note the STM32CubeIDE code displays characters 'ld' in the 3rd position of the output and the last value (indx) which should be 0 is not displays.

If I change z to 9876543210 the online compiler gives the correct result but STMCubeIDE prints the following: 50 100 ld 1286608618

What can I do to print a 64-bit int value?

Thanks,

Brian

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

STM32CubeIDE uses a reduced library by default in order to reduce code size, as it's usually preferred for embedded development. Part of this tradeoff is dropping support for long long ints in printf-type functions.

> What can I do to print a 64-bit int value?

Change the runtime library from reduced to standard.

0693W00000GY7bkQAD.png

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
TDK
Guru

STM32CubeIDE uses a reduced library by default in order to reduce code size, as it's usually preferred for embedded development. Part of this tradeoff is dropping support for long long ints in printf-type functions.

> What can I do to print a 64-bit int value?

Change the runtime library from reduced to standard.

0693W00000GY7bkQAD.png

If you feel a post has answered your question, please click "Accept as Solution".

Excellent! Thank you so much for this knowledge. I'll take a look at this tomorrow and keep the code size in mind.

Thanks again,

Regards,

Brian

Piranha
Chief II

Regarding large integers, code size etc.... Read my comments in this topic:

https://community.st.com/s/question/0D53W00001FT7OdSAL/problem-with-optimal-code-size