PRINTF %d doesn't handle negative numbers?

cancel
Showing results for 
Search instead for 
Did you mean: 

PRINTF %d doesn't handle negative numbers?

Jump to solution
1,911 Views
Contributor V

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.

 

Labels (1)
Tags (2)
0 Kudos
1 Solution
112 Views
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!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

4 Replies
112 Views
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 Kudos
113 Views
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!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

112 Views
Contributor V

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 Kudos
112 Views
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