1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
<title>printf format identifiers.</title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>printf format identifiers.</h1>
</center>
<hr>
<p>
printf formatting is controlled by 'format identifiers' which, are shown
below in their simplest form.
<p>
<center>
<table border bgcolor="ivory" width="80%">
<tr>
<td>
<pre>
%d %i Decimal signed integer.
%o Octal integer.
%x %X Hex integer.
%u Unsigned integer.
%c Character.
%s String. See below.
%f double
%e %E double.
%g %G double.
%p pointer.
%n Number of characters written by this printf.
No argument expected.
%% %. No argument expected.
</pre>
</td>
<tr>
</table>
</center>
<p>
These identifiers actually have upto 6 parts as shown in the table below.
They MUST be used in the order shown.
<p>
<center>
<table border bgcolor="ivory" width="80%">
<tr align=center>
<td><a href="#percent">%</a></td>
<td><a href="#flags">Flags</a></td>
<td><a href="#width">Minimum field width</a></td>
<td><a href="#period">Period</a></td>
<td><a href="#precision">Precision. Maximum field width</a></td>
<td><a href="#id">Argument type</a></td>
</tr>
<tr align=center>
<td>Required</td>
<td>Optional</td>
<td>Optional</td>
<td>Optional</td>
<td>Optional</td>
<td>Required</td>
</table>
</center>
<p>
<a name=percent>
<hr>
<h2>%</h2>
The % marks the start and therfore is manatory.
<p>
<a name=flags>
<hr>
<h2>Flags</h2>
The format identifers can be altered from their default function by
applying the following <b>flags</b>:
<p>
<center>
<table border bgcolor="ivory" width="80%">
<tr>
<td>
<pre>
- Left justify.
0 Field is padded with 0's instead of blanks.
+ Sign of number always O/P.
blank Positive values begin with a blank.
# Various uses:
%#o (Octal) 0 prefix inserted.
%#x (Hex) 0x prefix added to non-zero values.
%#X (Hex) 0X prefix added to non-zero values.
%#e Always show the decimal point.
%#E Always show the decimal point.
%#f Always show the decimal point.
%#g Always show the decimal point trailing
zeros not removed.
%#G Always show the decimal point trailing
zeros not removed.
</pre>
</td>
</tr>
</table>
</center>
<ul>
<li>The flags must follow the <a href="#percent">%</a>.
<li>Where it makes sense, more than one flag can be used.
</ul>
Here are a few more examples.
<p>
<center>
<table border bgcolor="ivory" width="80%">
<tr>
<td>
<pre>
printf(" %-10d \n", number);
printf(" %010d \n", number);
printf(" %-#10x \n", number);
printf(" %#x \n", number);
</pre>
</td>
</tr>
</table>
</center>
<p>
<a name=width>
<hr>
<h2>Minimum field width.</h2>
By default the width of a field will be the minimum required to hold
the data. If you want to increase the field width you can use the
following syntax.
<p>
<center>
<table border bgcolor="ivory" width="80%">
<tr>
<td>
<pre>
main()
{
int number = 5;
char *pointer = "little";
printf("Here is a number-%4d-and a-%10s-word.\n", number, pointer);
}
/*********************************
*
* Program result is:
*
* Here is a number- 5-and a- little-word.
*
*********************************/
</pre>
</td>
</tr>
</table>
</center>
<p>
As you can see, the data is right justified within the field. It can
be left justified by using the <a href="#flags">- flag</a>.
A <a href="#precision">maximum string</a> width can also be specified.
<p>
The width can also be given as a variable as shown below.
<p>
<center>
<table border bgcolor="ivory" width="80%">
<tr>
<td>
<pre>
main()
{
int number=5;
printf("---%*d----\n", 6, number);
}
/*********************************
*
* Program result is:
*
* ---- 5---
*
*********************************/
</pre>
</td>
</tr>
</table>
</center>
<p>
The * is replaced with the supplied <b>int</b> to provide the ability to
dynamically specify the field width.
<p>
<hr>
<a name=period>
<h2>Period</h2>
If you wish to specify the <a href="#precision">precision</a> of an argument,
it MUST be prefixed with the period.
<p>
<a name=precision>
<hr>
<h2>Precision</h2>
The Precision takes different meanings for the different format types.
<h3>Float Precision</h3>
<pre>
%8.2f
</pre>
This says you require a total field of 8 characters, within the 8
characters the last 2 will hold the decimal part.
<pre>
%.2f
</pre>
The example above requests the minimum field width and the last two
characters are to hold the decimal part.
<h3>Character String Maximum field width</h3>
The precision within a string format specifies the maximum
field width.
<pre>
%4.8s
</pre>
Specifies a minimum width of 4 and a maximum width
of 8 characters. If the string is greater than 8 characters,
it will be cropped down to size.
<p>
<img src=../../GRAPHICS/computer.gif>
<a href=../EXAMPLES/sprintf1.c>Here is a little program</a>
that shows an alternative to <a href=strncpy.html>strncpy</a>.
<h3>* Precision</h3>
As with the 'width' above, the precision does not have to be hard
coded, the * symbol can be used and an integer supplied to give its
value.
<p>
<hr>
<a name=id>
<h2>Format Identifiers</h2>
The format identifier describes the expected data. The identifier is the
character that ends
Here is a list of the format identifers as used in 'printf' ,'sprintf'
,'fprintf' and 'scanf'.
<ol>
<li>Except for '%' and 'n', all the identifiers expect to extract an argument
from the <b>printf</b> parameter list.
<li>All of the parmameters should be the value
to be inserted. EXCEPT %s, this expects a <a href="../CONCEPT/pointers.html">
pointer</a> to be passed.
</ol>
<h2>An example.</h2>
<p>
<center>
<table border bgcolor="ivory" width="80%">
<tr>
<td>
<pre>
main()
{
int number=5;
char *pointer="little";
printf("Here is a number %d and a %s word.\n", number, pointer);
}
/*********************************
*
* Program result is:
*
* Here is a number 5 and a little word.
*
*********************************/
</pre>
</td>
</tr>
</table>
</center>
<p>
<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html"> Top</a>
</td><td width="25%">
<a href="../master_index.html"> Master Index</a>
</td><td width="25%">
<a href="../SYNTAX/keywords.html"> Keywords</a>
</td><td width="25%">
<a href="funcref.htm"> Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie
</address><p>
</body>
</html>
|