我试图使用 sprintf 来显示一些值以用于调试目的。我在
STM32CubeIDE v1.5.0 中得到了错误的结果,但它正在使用在线 gcc 编译器。
代码在一个函数中,现在我正在重新分配函数内部的值以进行调试。这是我在 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 rn>", u, y, z, indx);
- HAL_UART_Transmit_IT(&huart3, (uint8_t *) msg, strlen(msg)); // &huart3 &hlpuart1
- }
结果显示:
50 100 磅 4351234
>
在在线 gcc 编译器中,我运行这个程序:
- // Online C compiler to run C program online
- #include
- #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 rn>", u, y, z, indx);
- printf("Hello world %s" , msg);
- return 0;
- }
显示为:Hello world 50 100 4351234 0
一切都如我所料。
请注意,STM32CubeIDE 代码在输出的第 3 个位置显示字符“ld”,并且不显示应为 0 的最后一个值 (indx)。
如果我将 z 更改为 9876543210,在线编译器会给出正确的结果,但 STMCubeIDE 会打印以下内容:50 100 ld 1286608618
如何打印 64 位 int 值?