Adding floating point support for printf() and scanf() to your project

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

Adding floating point support for printf() and scanf() to your project

Jump to solution
3,687 Views
robertpoor
Senior Contributor I

Note: this is a "howto" question and answer rather than a real question, given because it may be helpful to others.

 

Description of the Problem: printf("%f", 2.0) doesn't print anything, nor does it give any errors.

Labels (1)
0 Kudos
1 Solution
2,358 Views
robertpoor
Senior Contributor I

Note: this is a "howto" response rather than a genuine question, given because it may be helpful to others.

Disclaimer: The information here has been cobbled together from several sources and experimentation.  It works for my environment (KDS 3.2.0 running under Mac OSX 10.12.2, Cross ARM C compiler), but details may be different for your environment.

Description of the Problem: printf("%f", 2.0) doesn't print anything.

Reason: The Cross ARM C compiler (and linker) do not provide support for printing and scanning floating point numbers by default.

Solution: You can add support to the Cross ARM C compiler for printing and scanning floating point numbers (and doubles) as follows.

  1. In KDS, under the Project menu item, click on Properties
  2. Expand the C/C++ Build item in the left hand column
  3. Click on C/C++ Build Settings in the left hand column
  4. Click on [Tool Settings] tab in the right hand panel
  5. Expand the Cross Arm C Compiler item
  6. Click on Cross Arm C Compiler / Preprocessor
  7. In the Defined Symbols (-D) panel, click on the + icon
  8. In the dialog panel that appears, type "PRINTF_FLOAT_ENABLE=1" and [OKAY]
  9. Click on the + icon again
  10. In the dialog panel that appears, type "SCANF_FLOAT_ENABLE=1" and [OKAY]
  11. Click [Apply]
  12. Expand the Cross Arm C++ Linker item
  13. Click on Cross Arm C++ Linker / Miscellaneous
  14. In the right hand panel, enable the two check boxes labeled "Use float with nano printf (-u _printf_float)" and "Use float with nano scanf (-u _scanf_float)".
  15. Click [Apply]
  16. Click [OK]

You can test that printf() works with floats by creating a project based on the "Hello World" demo application and modifying it as follows:

#include "fsl_device_registers.h"

#include "fsl_debug_console.h"

#include "board.h"

#include "pin_mux.h"

#include "clock_config.h"

int main(void{

    char ch;

    double val;

    BOARD_InitPins();

    BOARD_BootClockRUN();

    BOARD_InitDebugConsole();

    val = 0.01;

    for (int i=0; i<5; i++) {

      PRINTF("val = %f\n", val);

      val *= 10.0;

    }

    while (1) {

        ch = GETCHAR();

        PUTCHAR(ch);

    }

}

If all goes well, you should see this printed on the console:

val = 0.010000
val = 0.100000
val = 1.000000
val = 10.000000
val = 100.000000

View solution in original post

0 Kudos
1 Reply
2,359 Views
robertpoor
Senior Contributor I

Note: this is a "howto" response rather than a genuine question, given because it may be helpful to others.

Disclaimer: The information here has been cobbled together from several sources and experimentation.  It works for my environment (KDS 3.2.0 running under Mac OSX 10.12.2, Cross ARM C compiler), but details may be different for your environment.

Description of the Problem: printf("%f", 2.0) doesn't print anything.

Reason: The Cross ARM C compiler (and linker) do not provide support for printing and scanning floating point numbers by default.

Solution: You can add support to the Cross ARM C compiler for printing and scanning floating point numbers (and doubles) as follows.

  1. In KDS, under the Project menu item, click on Properties
  2. Expand the C/C++ Build item in the left hand column
  3. Click on C/C++ Build Settings in the left hand column
  4. Click on [Tool Settings] tab in the right hand panel
  5. Expand the Cross Arm C Compiler item
  6. Click on Cross Arm C Compiler / Preprocessor
  7. In the Defined Symbols (-D) panel, click on the + icon
  8. In the dialog panel that appears, type "PRINTF_FLOAT_ENABLE=1" and [OKAY]
  9. Click on the + icon again
  10. In the dialog panel that appears, type "SCANF_FLOAT_ENABLE=1" and [OKAY]
  11. Click [Apply]
  12. Expand the Cross Arm C++ Linker item
  13. Click on Cross Arm C++ Linker / Miscellaneous
  14. In the right hand panel, enable the two check boxes labeled "Use float with nano printf (-u _printf_float)" and "Use float with nano scanf (-u _scanf_float)".
  15. Click [Apply]
  16. Click [OK]

You can test that printf() works with floats by creating a project based on the "Hello World" demo application and modifying it as follows:

#include "fsl_device_registers.h"

#include "fsl_debug_console.h"

#include "board.h"

#include "pin_mux.h"

#include "clock_config.h"

int main(void{

    char ch;

    double val;

    BOARD_InitPins();

    BOARD_BootClockRUN();

    BOARD_InitDebugConsole();

    val = 0.01;

    for (int i=0; i<5; i++) {

      PRINTF("val = %f\n", val);

      val *= 10.0;

    }

    while (1) {

        ch = GETCHAR();

        PUTCHAR(ch);

    }

}

If all goes well, you should see this printed on the console:

val = 0.010000
val = 0.100000
val = 1.000000
val = 10.000000
val = 100.000000

0 Kudos