Printf

From Pinguino
Jump to: navigation, search


Name

printf - print formatted string

Description

Printf functions (Serial.printf, CDC.printf, lcd.printf, ...) send a formatted string to the current output.

It uses printf.c, a new implementation of the stdlib library (stdlib.c).

This new library provides simple, small and low footprint printf functionality for use in embedded systems.

The formats supported by this implementation are: 'd' 'u' 'b' 'o' 'c' 's' 'x' 'X' and 'f'.

Zero padding and field width are also supported.

Formats

'd' signed int
Serial.printf("i = %d\n", -128);
Output on serial port : i = -128
'u' unsigned int
lcd.printf("i = %u\n", 1024);
Output on LCD display : i = 1024
'b' binary
CDC.printf("i = %b\n", 1024);
Output on USB CDC (serial emulation) port : i = 010000000000
'o' octal
Serial.printf("i = %o\n", 1024);
Output on default serial port : i = 2000
'x' lower case hexadecimal
lcd.printf("i = %x\n", 8191);
Output on LCD display : i = 1fff
'X' upper case hexadecimal
CDC.printf("i = %X\n", 8191);
Output on USB CDC port : i = 1FFF
'c' ASCII character
Serial.printf("c = %c\n", 65);
Output on serial port : c = A
's' string
lcd.printf("My name is %s, James Bond\n", "Bond");
Output on LCD display : My name is Bond, James Bond
'f' float
CDC.printf("i = %f\n", 3.14);      // 2 digits after decimal comma by default
CDC.printf("i = %.1f \r\n", 3.14); // 1 digit  after decimal comma
Output on USB CDB port : i = 3.14 or i = 3.1

Zero padding and field width

See example below for Serial output.

Example

char *string = "Hello world!";
char c = 65;
int i = 333;
long l = 115200;
float f = 3.14159265;
 
void setup()
{
	Serial.begin(9600);
}

void loop()
{
	Serial.printf("\r\n");
	Serial.printf("**************************\r\n");
	Serial.printf("*** Serial Printf Demo ***\r\n");
	Serial.printf("**************************\r\n");
	Serial.printf("\r\n");
	Serial.printf("string = %s\r\n", string);
	Serial.printf("character = \"%c\"\r\n", c);
	Serial.printf("signed char = %d / unsigned char = %u\r\n", -c, -c);
	Serial.printf("signed int = %d / unsigned int = %u\r\n", -i, -i);
	Serial.printf("signed long = %ld / unsigned long = %lu\r\n", -l, -l);
	Serial.printf("decimal[%d] = hexa[0x%X] = binary[0b%016b] = octal[%o]\r\n", i, i, i, i);
	Serial.printf("float = %f\r\n", f);
	Serial.printf("justif: \"%-10s\"\r\n", "left");
	Serial.printf("justif: \"%10s\"\r\n", "right");
	Serial.printf(" 3: %04d zero padded\r\n", 3);
	Serial.printf(" 3: %-4d left justif.\r\n", 3);
	Serial.printf(" 3: %4d right justif.\r\n", 3);
	Serial.printf("-3: %04d zero padded\r\n", -3);
	Serial.printf("-3: %-4d left justif.\r\n", -3);
	Serial.printf("-3: %4d right justif.\r\n", -3);
	Serial.printf("\r\n");
	Serial.printf("Press Any Key to continue ...\r\n");
	Serial.getkey();
	delay(5000);
}

Bugs

On 8-bit PIC with X.4 rev.685, 'pad' and 'justify' directives seem not to work.

See also

Printf for all supported formats.
Sprintf

CDC.getString        CDC.print        CDC.printf
CDC.println          CDC.read         CDC.write
CDC.getKey

Serial.getString     Serial.print     Serial.printf
Serial.println       Serial.read      Serial.write

lcd.print

Comments

Since: Beta 9.5