From SEGGER Wiki
Jump to: navigation, search

"printf" is a function in the C-standard library that outputs text to standard output. Standard output is typically the terminal or debug console, depending on the system the program is running on. The f stands for formatted, allowing the function to output not just fixed strings, but also text with variable data in it.

Hello world

The simplest of all C-programs is known as "Hello world" program. It outputs "Hello world", using printf.

#include <stdio.h>

int main(void) {
  printf("Hello world!\n");
  return 0;

Using parameters

Parameters need to be specified in the format string. A parameter definition starts with a % character. A simple example with one numerical parameter looks as below:

  printf("Total sum is: %d\n", Sum);

Parameter specification

Type field

The Type field can be any of:

Character Description
% Prints a literal % character (this type doesn't accept any flags, width, precision, length fields).
d, i int as a signed decimal number. %d and %i produce identical output.)
u Print decimal unsigned int.
f, F double in normal (fixed-point) notation.
x, X unsigned int as a hexadecimal number. x uses lower-case letters and X uses upper-case.
s null-terminated string.
c char (character).


Formatting means replacing the parameter placeholders, such as %d or %s, by the actual values. In most systems, this is done by the target. In larger systems, this is fine, but it comes at a price: The formatting function needs to do the parsing and needs to know all parameter types. This results in a lot of code, basically used for terminal out, so for debugging purposes. The amount of code varies, depending on which functionality is actually supported by the formatter, but is in general between about 3kB and up to 20 kB. In many IDEs, the capabilities of the formatter can be selected, in order to choose a formatter with just the required capabilities with minimum size. Some IDEs such as Rowley's Crossworks and SEGGER's Embedded Studio also come with the ability to do host-side formatting. With Host-side or host-based formatting, the host reads the parameters and performs the formatting on the host, which eliminates the requirement to have the formatting code on the target. This is a huge advantage for smaller targets, especially Microcontrollers with limited amount of Program memory (Flash memory).