Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
Library Functions
 2012 Microchip Technology Inc.
DS52053B-page 351
The 
printf()
 routine is passed a format string, followed by a list of zero or more argu-
ments. In the format string are conversion specifications, each of which is used to print 
out one of the argument list values.
Each conversion specification is of the form 
%m.nc
 where the percent symbol 
%
 intro-
duces a conversion, followed by an optional width specification 
m
. The 
n
 specification 
is an optional precision specification (introduced by the dot) and 
c
 is a letter specifying 
the type of the conversion.
A minus sign (’-’) preceding 
m
 indicates left rather than right adjustment of the converted 
value in the field. Where the field width is larger than required for the conversion, blank 
padding is performed at the left or right as specified. Where right adjustment of a 
numeric conversion is specified, and the first digit of 
m
 is 0, then padding will be per-
formed with zeroes rather than blanks. For integer formats, the precision indicates a 
minimum number of digits to be output, with leading zeros inserted to make up this 
number if required.
A hash character (
#
) preceding the width indicates that an alternate format is to be 
used. The nature of the alternate format is discussed below. Not all formats have 
alternates. In those cases, the presence of the hash character has no effect.
If the character 
*
 is used in place of a decimal constant, e.g., in the format 
%*d
, then 
one integer argument will be taken from the list to provide that value. 
The types of conversion are:
f
Floating point - 
m
 is the total width and 
n
 is the number of digits after the decimal 
point. If 
n
 is omitted it defaults to 6. If the precision is zero, the decimal point will be 
omitted unless the alternate format is specified.
e
Print the corresponding argument in scientific notation. Otherwise similar to 
f
.
Use 
e
 or 
f
 format, whichever gives maximum precision in minimum width. Any trail-
ing zeros after the decimal point will be removed, and if no digits remain after the 
decimal point, it will also be removed.
o x X u d
 Integer conversion - in radices 8, 16, 16, 10 and 10 respectively. The con-
version is signed in the case of 
d
, unsigned otherwise. The precision value is the total 
number of digits to print, and may be used to force leading zeroes. For example, 
%8.4x
 
will print at least 4 HEX digits in an 8 wide field. Preceding the key letter with an 
l
 indi-
cates that the value argument is a long integer. The letter 
X
 prints out hexadecimal 
numbers using the upper case letters A-F rather than a- f as would be printed when 
using 
x
. When the alternate format is specified, a leading zero will be supplied for the 
octal format, and a leading 0x or 0X for the HEX format.
s
Print a string - the value argument is assumed to be a character. At most 
n
 
characters from the string will be printed, in a field 
m
 characters wide.
c
The argument is assumed to be a single character and is printed literally.
Any other characters used as conversion specifications will be printed. Thus 
%
 will 
produce a single percent sign.
The 
vprintf()
 function is similar to 
printf()
 but takes a variable argument list rather 
than a list of arguments. See the description of va_start() for more information on 
variable argument lists. An example of using 
vprintf()
 follows.