blob: 0ae4b62de3b0866419ae8fab57b47479d0257ca8 [file] [log] [blame]
<html lang="en">
<head>
<title>Printf Extension Example - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Customizing-Printf.html#Customizing-Printf" title="Customizing Printf">
<link rel="prev" href="Defining-the-Output-Handler.html#Defining-the-Output-Handler" title="Defining the Output Handler">
<link rel="next" href="Predefined-Printf-Handlers.html#Predefined-Printf-Handlers" title="Predefined Printf Handlers">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Printf-Extension-Example"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Predefined-Printf-Handlers.html#Predefined-Printf-Handlers">Predefined Printf Handlers</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Defining-the-Output-Handler.html#Defining-the-Output-Handler">Defining the Output Handler</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Customizing-Printf.html#Customizing-Printf">Customizing Printf</a>
<hr>
</div>
<h4 class="subsection">12.13.4 <code>printf</code> Extension Example</h4>
<p>Here is an example showing how to define a <code>printf</code> handler function.
This program defines a data structure called a <code>Widget</code> and
defines the &lsquo;<samp><span class="samp">%W</span></samp>&rsquo; conversion to print information about <code>Widget&nbsp;*</code><!-- /@w -->
arguments, including the pointer value and the name stored in the data
structure. The &lsquo;<samp><span class="samp">%W</span></samp>&rsquo; conversion supports the minimum field width and
left-justification options, but ignores everything else.
<pre class="smallexample"> #include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;printf.h&gt;
typedef struct
{
char *name;
}
Widget;
int
print_widget (FILE *stream,
const struct printf_info *info,
const void *const *args)
{
const Widget *w;
char *buffer;
int len;
/* <span class="roman">Format the output into a string.</span> */
w = *((const Widget **) (args[0]));
len = asprintf (&amp;buffer, "&lt;Widget %p: %s&gt;", w, w-&gt;name);
if (len == -1)
return -1;
/* <span class="roman">Pad to the minimum field width and print to the stream.</span> */
len = fprintf (stream, "%*s",
(info-&gt;left ? -info-&gt;width : info-&gt;width),
buffer);
/* <span class="roman">Clean up and return.</span> */
free (buffer);
return len;
}
int
print_widget_arginfo (const struct printf_info *info, size_t n,
int *argtypes)
{
/* <span class="roman">We always take exactly one argument and this is a pointer to the
structure..</span> */
if (n &gt; 0)
argtypes[0] = PA_POINTER;
return 1;
}
int
main (void)
{
/* <span class="roman">Make a widget to print.</span> */
Widget mywidget;
mywidget.name = "mywidget";
/* <span class="roman">Register the print function for widgets.</span> */
register_printf_function ('W', print_widget, print_widget_arginfo);
/* <span class="roman">Now print the widget.</span> */
printf ("|%W|\n", &amp;mywidget);
printf ("|%35W|\n", &amp;mywidget);
printf ("|%-35W|\n", &amp;mywidget);
return 0;
}
</pre>
<p>The output produced by this program looks like:
<pre class="smallexample"> |&lt;Widget 0xffeffb7c: mywidget&gt;|
| &lt;Widget 0xffeffb7c: mywidget&gt;|
|&lt;Widget 0xffeffb7c: mywidget&gt; |
</pre>
</body></html>