AnsweredAssumed Answered

PRINTF %d doesn't handle negative numbers?

Question asked by Robert Poor on Jan 16, 2017
Latest reply on Jan 21, 2017 by ZhangJennie

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.

 

Outcomes