Print Floating Point Numbers in AVR C with Atmel Studio 7

Created on: 30 March 2016

When trying to print floating point numbers of type float using sprintf() or printf() functions in an AVR 8-bit C program using Atmel Studio 7, the number does not print correctly. Instead of the float being printed to a string or standard output, a question mark is printed.

Example output:

flt_num = ?

The reason that floating point numbers are not printed is because the default settings in Atmel Studio disable them for sprintf / printf type functions to save microcontroller memory.

The solution to this problem is to change some linker settings so that the floating point number is printed as expected.

AVR Memory Usage

After building a simple C program in Atmel Studio 7 that uses sprintf() to print a floating point number to a string and send it out of the serial port (USART0) of an ATmega2560 microcontroller, the following memory usage was recorded.

1) Flash: 1,874 bytes    RAM: 16 bytes
2) Flash: 3,384 bytes    RAM: 16 bytes

Where 1) above is before changing linker settings to enable printing of floating point numbers (output printed to string is: flt_num = ?); and 2) is after changing the linker options as described below (output printed to string is: flt_num = 31.8394).

The test code used is shown in the following listing.

int main(void)
{
    char out_str[30] = {0};     // string to print to and transmit
    float flt_num = 31.8394;    // float number to print to string

    UartInit();

    sprintf(out_str, "flt_num = %f\r\n", flt_num);
    UartTxStr(out_str);

    while (1) {
    }
}

Changing Linker Settings to Print Floating Point Numbers in Atmel Studio 7

In Atmel Studio 7 on the top menu, click Project → <project name> Properties... to bring up the properties page for the currently open project. The image below shows the menu in Atmel Studio 7 for a project named print_float_mega_2560.

Opening Atmel Studio 7 Project Settings
Opening Atmel Studio 7 Project Settings

Click Toolchain in the page at the left of the project properties page and then General under the AVR/GNU Linker item as shown in the image below. Finally check the Use vprintf library(-Wl,-u,vfprintf).

Selecting the vprintf Library under the Linker Settings of the Atmel Studio 7 Project Properties Page
Selecting the vprintf Library under the Linker Settings of the Atmel Studio 7 Project Properties Page

Now click Miscellaneous under the AVR/GNU Linker item and add the following in the Other Linker Flags box as shown in the following image.

-lprintf_flt
Adding linker flags to the AVR/GNU linker in Atmel Studio 7
Adding Linker Flags to the AVR/GNU Linker in Atmel Studio 7

Save the changes to the linker options (Ctrl + S) and then rebuild the project. Projects that use sprintf and printf type functions should now be able to print floating point numbers to strings or standard output.

What the Linker Flags Mean

-Wl,-u,vfprintf

-Wl means that linker options follow with each linker option separated by a comma.

-u forces linking of library module that follows it.

vfprintf function which is used by printf type functions. Used with the -u option forces the linker to link this function from the library which is necessary to use printf(), sprintf(), etc.

-lprintf_flt

-lprintf_flt means that the full functionality including floating point conversion will be enabled for printf type functions.