PRINTF %d doesn't handle negative numbers?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

PRINTF %d doesn't handle negative numbers?

跳至解决方案
14,223 次查看
robertpoor
Senior Contributor I

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.

 

标签 (1)
标记 (2)
0 项奖励
回复
1 解答
12,424 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

Hello Robert,

to print negative number, please:

1.enable PRINTF_ADVANCED_ENABLE=1 in preprocessor setting.

pastedImage_1.png

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!
-----------------------------------------------------------------------------------------------------------------------

在原帖中查看解决方案

8 回复数
3,245 次查看
ugljesa_tomanov
Contributor I

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

0 项奖励
回复
3,231 次查看
ugljesa_tomanov
Contributor I
Problem solved.
When PRINTF_ADVANCED_ENABLED is set to 1 it increases the needed amount of RAM so my task was running in to stack overflow condition.
0 项奖励
回复
11,033 次查看
cmou
Contributor I

Amazing for me.

0 项奖励
回复
12,423 次查看
danielcolon
Contributor II

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.

pastedImage_1.png

0 项奖励
回复
12,425 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

Hello Robert,

to print negative number, please:

1.enable PRINTF_ADVANCED_ENABLE=1 in preprocessor setting.

pastedImage_1.png

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!
-----------------------------------------------------------------------------------------------------------------------

10,926 次查看
Sergio_Carrato
Contributor I


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

0 项奖励
回复
12,424 次查看
robertpoor
Senior Contributor I

That appears to work, thank you.  (But I must point out that it's strange to enable FLOATING POINT in order to print out a negative INTEGER.)

0 项奖励
回复
12,424 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

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