summaryrefslogtreecommitdiff
path: root/reference/C/FUNCTIONS/format.html
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2008-01-27 11:37:44 +0100
committerTobias Klauser <tklauser@xenon.tklauser.home>2008-01-27 11:37:44 +0100
commit7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch)
treeb1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/FUNCTIONS/format.html
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/FUNCTIONS/format.html')
-rw-r--r--reference/C/FUNCTIONS/format.html315
1 files changed, 315 insertions, 0 deletions
diff --git a/reference/C/FUNCTIONS/format.html b/reference/C/FUNCTIONS/format.html
new file mode 100644
index 0000000..0d18e69
--- /dev/null
+++ b/reference/C/FUNCTIONS/format.html
@@ -0,0 +1,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>