PRINTF %d doesn't handle negative numbers?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PRINTF %d doesn't handle negative numbers?

Jump to solution
15,125 Views
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.

 

Labels (1)
Tags (2)
0 Kudos
Reply
1 Solution
13,326 Views
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!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

8 Replies
4,147 Views
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 Kudos
Reply
4,133 Views
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 Kudos
Reply
11,935 Views
cmou
Contributor I

Amazing for me.

0 Kudos
Reply
13,325 Views
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 Kudos
Reply
13,327 Views
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!
-----------------------------------------------------------------------------------------------------------------------

11,828 Views
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 Kudos
Reply
13,326 Views
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 Kudos
Reply
13,326 Views
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