Site hosted by Angelfire.com: Build your free website today!
 City & Guilds 7261/224 Coding and Programming in C II 

PRINTF

1 INTRODUCTION

The printf function provides formatted output conversion. Its format is:

   printf("format-string", arguments); 
Format-string contains two types of objects: ordinary characters, including escape sequences (Constants, Section 4), which are copied to the output stream, and conversion specifications, which cause each successive argument to be converted and printed. If no conversion specifications are present, no arguments are required.

2 CONVERSION SPECIFICATION

A conversion specification begins with the character % and ends with a conversion character (see under type). The syntax of a conversion specification is:

   %[flags][width][.precision]type
Brackets [] are my way of showing you fields that are optional.. The minimum form a conversion specification can take is &type.

2.1 Type (conversion character); one of:
d print int argument as signed decimal integer
x, X print int argument as unsigned hexadecimal integer, using abcdef for x or ABCDEF for X
c print int or char argument as a single character
s print string argument as a string of characters
f print float argument in format [-]mmm.dddddd
% no argument is converted, print a %

For example:

   int i = 4;
   printf("i has the value %d\n", i);
will display:
   i has the value 4
2.2 Flags (in any order), which modify the specification; the two most useful are:
- left justify the converted argument
0 pad to field width with leading zeros
2.3 Width: a number specifying a minimum field width. The converted argument is printed in a field as least this wide. Default padding occurs on the left, using spaces, to make up the field width: on the right, if the left adjustment flag, -, has been used. Zeros can be used to pad if the zero padding flag (0) is present.

2.4 Precision: this is prefixed by a full stop; it is a number specifying different things depending on type (see Section 2.1):
s the maximum number of characters to be printed
f the number of digits to be printed after the decimal point
d, x, X the minimum number of digits, padding with leading zeros if necessary
/* printf: Practical Example 1 */
#include 

int main(void)
{
   int i, x = 3;
   char ch = 'k';

   puts("----------------------------------------------"); /* 46 hyphens */
   printf("|%.3s|\n", "ABCDEF");
   printf("|%10s|\n", "ABCDEF");
   printf("|%10.3s|\n", "ABCDEF");
   printf("|%-10.3s|\n", "ABCDEF");

   printf("|%2d|\n", x);
   printf("|%.2d|\n", x);

   i = 124;

   printf("|%.4X|\n", i);
   printf("|%04X|\n", i);
   printf("%c\n", 'h');
   printf("%c\t%X\t%d\n", ch, ch, ch);
   printf("|%4.2f|\n", 2.345);
   printf("|%6.2f|\n", 23.456);
   return 0;
}  /* main */

EXERCISE 1

1 Use printf to display the integer 12 in a decimal field two characters wide, and a hexadecimal field two characters wide, separated by a tab. The hexadecimal field should be padded with leading zeros.
2 Write down the conversion specification to display a float to two decimal places.

Back to C Home Page


Martin O'Brien <martin.o_brien@which.net>

Last update: 9 May 1999