printf()

Output formatted text.

  #include <stdio.h>
  
  int printf(const char *format, ...);    
Parameter

format

A pointer to a character string containing format information.

Remarks

The printf() function outputs formatted text. The function takes one or more arguments, the first being format, a character array pointer. The optional arguments following format are items (integers, characters, floating point values, etc.) that are to be converted to character strings and inserted into the output of format at specified points.

The printf() function sends its output to stdout.

printf() returns the number of arguments that were successfully output or returns a negative value if it fails.

Listing: Example of printf() usage

#include <stdio.h>

int main(void)

{

int i = 25;

char c = 'M';

short int d = 'm';

static char s[] = "woodworking!";

static char pas[] = "\pwoodworking again!";

float f = 49.95;

double x = 1038.11005;

int count;

printf("%s printf() demonstration:\n%n", s, &count);

printf("The last line contained %d characters\n",count);

printf("Pascal string output: %#20s\n", pas);

printf("%-4d %x %06x %-5o\n", i, i, i, i);

printf("%*d\n", 5, i);

printf("%4c %4u %4.10d\n", c, c, c);

printf("%4c %4hu %3.10hd\n", d, d, d);

printf("$%5.2f\n", f);

printf("%5.2f\n%6.3f\n%7.4f\n", x, x, x);

printf("%*.*f\n", 8, 5, x);

return 0;

}

The output is:

woodworking! printf() demonstration:

The last line contained 37 characters

Pascal string output: woodworking again!

25 19 000019 31

25

M 77 0000000077

m 109 0000000109

$49.95

1038.11

1038.110

1038.1101

1038.11005
Listing: Example of AltiVec printf Extensions

#include <stdio.h>

int main(void)

{

vector signed char s =

(vector signed char)(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);

vector unsigned short us16 =

(vector unsigned short)('a','b','c','d','e','f','g','h');

vector signed int sv32 =

(vector signed int)(100, 2000, 30000, 4);

vector signed int vs32 =

(vector signed int)(0, -1, 2, 3);

vector float flt32 =

(vector float)(1.1, 2.22, 3.3, 4.444);

printf("s = %vd\n", s);

printf("s = %,vd\n", s);

printf("vector=%@vd\n", "\nvector=", s);

// c specifier so no space is added.

printf("us16 = %vhc\n", us16);

printf("sv32 = %,5lvd\n", sv32);

printf("vs32 = 0x%@.8lvX\n", ", 0x", vs32);

printf("flt32 = %,5.2vf\n", flt32);

return 0;

}

The output is:

s = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

s = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16

vector=1

vector=2

vector=3

vector=4

vector=5

vector=6

vector=7

vector=8

vector=9

vector=10

vector=11

vector=12

vector=13

vector=14

vector=15

vector=16

us16 = abcdefgh

sv32 = 100, 2000,30000, 4

vs32 = 0x00000000, 0xFFFFFFFF, 0x00000002, 0x00000003

flt32 = 1.10, 2.22, 3.30, 4.44