It appears that PRINTF and related methods don't handle negative numbers:
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"#include "pin_mux.h"
#include "clock_config.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Prototypes
******************************************************************************//*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Main function
*/
int main(void) {
char ch;
int32_t int32;
/* Init board hardware. */
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
int32 = -2;
PRINTF("int32 = 0x%x (%d)\n", int32, int32);
while (1) {
ch = GETCHAR();
PUTCHAR(ch);}
}
The above program prints out:
int32 = 0xfffffffe (2)
...rather than 0xfffffffe (-2) as I would expect.
Am I missing something? Or is this behavior documented somewhere?
PS: I'm running KDS 3.2.0 on Mac OSX 10.12.2, compiling for a FRDM-KL27Z board.
已解决! 转到解答。
Hello Robert,
to print negative number, please:
1.enable PRINTF_ADVANCED_ENABLE=1 in preprocessor setting.
2. remove bracket from %d
PRINTF("int32 = 0x%x %d\n", int32, int32);
see attached video.
Have a great day,
Jennie Zhang
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi,
I am facing the similar issue on LPC55. I am using MCUXpresso v11.9.0 [Build 2144] and SDK version 2.15.
I have redirected the PRINTF to the SWO output.
If PRINTF_ADVANCED_ENABLE is set to 0, printing works but negative numbers are written as positive.
If I set PRINTF_ADVANCED_ENABLE to 1, after the first printf with a number argument is reached no printfs are working afterwards.
My code is executing several printfs with just text followed by some printfs with numbers.
Best regards
Ugljesa
I had this same problem using KL25Z but rather than modify the Preprocessor option like in the picture above, I found out the same definition within fsl_debug_console.h. Before it was defines as "#define PRINTF_ADVANCED_ENABLE 0U", I just change the 0 by 1 and the negative numbers started to appear =).
However I still have a doubt for the SDK staff... Which ones are all of those ADVANCED features that got enabled with this define?
Thanks.
Hello Robert,
to print negative number, please:
1.enable PRINTF_ADVANCED_ENABLE=1 in preprocessor setting.
2. remove bracket from %d
PRINTF("int32 = 0x%x %d\n", int32, int32);
see attached video.
Have a great day,
Jennie Zhang
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Dear Jennie,
thank you very much, the problem is solved.
However, I have a question: why does this option exist? I mean,
an option which, if not set, allows printf to print
"wrong" numbers (i.e. negative numbers which appear to be positive)?
I thank you in advance
Sergio
thanks for your feedback.
I just checked the screenshot, the correct setting is PRINTF_ADVANCED_ENABLE=1.
I just modified the screenshot to the correct one.
the video use setting of PRINTF_ADVANCED_ENABLE=1. it's right.
Thanks.
Best Regards,
Jennie Zhang