*.printf Formats

From Pinguino
Jump to: navigation, search

Description

There are various *.printf() functions in Pinguino which use printf.c, an alternative implementation of the stdlib library (stdlib.c), to format data for output. This new library provides simple and small printf functionality for use in embedded systems.

The functions that make use of the Pinguino printf.c library are :-

The Pinguino printf.c library supports the following format specifiers/identifiers: 'd' 'u' 'b' 'o' 'c' 's' 'x' 'X' and 'f'. Zero padding and field width are also supported.

The function makes use of a format string containing one or more format tags and other text to be included in the output.

The format tags take the form : %[flag][width]specifier

Specifiers

Specifier

Sample Code

Output

'd'

signed int

Serial.printf("i = %d\n", -128);

i = -128

'u'

unsigned int

Serial.printf("i = %u\n", 1024);

i = 1024

'b'

binary

Serial.printf("i = %b\n", 1024);

i = 100000000000

'o'

octal

Serial.printf("i = %o\n", 1024);

i = 2000

'x'

lower case hexadecimal

Serial.printf("i = %x\n", 8191);

i = 0fff

'X'

upper case hexadecimal

Serial.printf("i = %x\n", 8191);

i = 0FFF

'c'

ASCII character

Serial.printf("c = %c\n", 65);

c = A

's'

string

Serial.printf("My name is %s, James Bond\n", "Bond");

My name is Bond, James Bond

'f'

float

Serial.printf("f = %f\n", 3.14);

f = 3.14

Flags / Width


Flag/Width

Example Code

Output

(number) =
Right Justified

Serial.printf("i = %6dText\n", 128);
Serial.printf("i = %6dText\n", -128);

i = ___128Text
i = __-128Text

'-'

- (number) =
Left Justified

Serial.printf("i = %-6dText\n", 128);
Serial.printf("i = %-6dText\n", -128);

i = 128___Text
i = -128__Text

'0'

0 (number) =
Zero Padding

Serial.printf("i = %06d\n", 128)
Serial.printf("i = %06d\n", -128);

i = 000128
i = -00128

Escape Sequences

Certain output devices, e.g. CDC and serial, also support non-printing characters such as [carriage return] and [new line] by including the appropriate "escape" sequences in the format string.

\n = new line
\r = carriage return

Zero padding and field width

See example below.

Example

char *string = "Hello world!";
char c = 65;
int i = 333;
long l = 115200;
float f = 3.14159265; // floating point support is not yet implemented in printf

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 = %d / unsigned long = %u\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();
}

Bugs

See also

Comments