Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 5.11Formatted I/O 161


Conversion Description
type
d,i signed decimal
o unsigned octal
u unsigned decimal
x,X unsigned hexadecimal
f,F doublefloating-point number
e,E doublefloating-point number in exponential format
g,G interpreted as f, F, e, or E, depending on value converted
a,A doublefloating-point number in hexadecimal exponential format
c character (withllength modifier,wide character)
s string (withllength modifier,wide character string)
p pointer to avoid
n pointer to a signed integer into which is written the number of characters written so far
% a%character
C wide character(XSI option, equivalent tolc)
S wide character string(XSI option, equivalent tols)

Figure 5.9The conversion type component of a conversion specification

representing thenth argument. Note, however,that the two syntaxes can’t be mixed in
the same format specification. With the alternative syntax, arguments arenumbered
starting at one. If either the field width or precision is to be supplied by an argument,
the asterisk syntax is modified to*m$,wheremspecifies the position of the argument
supplying the value.
The following five variants of theprintffamily aresimilar to the previous five,
but the variable argument list (...) is replaced witharg.

#include <stdarg.h>
#include <stdio.h>
int vprintf(const char *restrictformat,va_list arg);

int vfprintf(FILE *restrictfp,const char *restrictformat,
va_listarg);

int vdprintf(intfd,const char *restrictformat,va_list arg);
All three return: number of characters output if OK, negative value if output error
int vsprintf(char *restrictbuf,const char *restrictformat,
va_listarg);
Returns: number of characters stored in array if OK, negative value if encoding error
int vsnprintf(char *restrictbuf,size_tn,
const char *restrict format,va_list arg);
Returns: number of characters that would have been stored in array
if buffer was large enough, negative value if encoding error

We use thevsnprintffunction in the error routines in Appendix B.
Free download pdf