<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
 lang="en" dir="ltr">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
    printf    [C++ Reference]
  </title>

  <meta name="generator" content="DokuWiki Release 2009-12-25c &quot;Lemming&quot;" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2010-04-08T07:16:41-0700" />
<meta name="keywords" content="c,io,printf" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/c/io/printf?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&amp;ns=c:io" />
<link rel="edit" title="Edit this page" href="/wiki/c/io/printf?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/c/io/printf" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/c/io/printf" />
<link rel="canonical" href="http://www.cppreference.com/wiki/c/io/printf" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&amp;t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&amp;t=custom1&amp;tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='c:io';var JSINFO = {"id":"c:io:printf","namespace":"c:io"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>

  <link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />

  </head>

<body>
<div class="dokuwiki">
  
  <div class="stylehead">

    <div class="breadcrumbs">
      <span class="bchead">You are here: </span><a href="../../start.html"  title="start">C++ Reference</a> &raquo; <a href="../../c/start.html"  title="c:start">The Standard C Library</a> &raquo; <a href="../../c/io/start.html"  title="c:io:start">Standard C I/O</a> &raquo; <a href="../../c/io/printf.html"  title="c:io:printf">printf</a>    </div>
    
  </div>


  
  
  <div class="page">

    <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
    <!-- wikipage start -->
    <!-- TOC START -->
<div class="toc">
<div class="tocheader toctoggle" id="toc__header">Table of Contents</div>
<div id="toc__inside">

<ul class="toc">
<li class="clear">

<ul class="toc">
<li class="level2"><div class="li"><span class="li"><a href="#printf" class="toc">printf</a></span></div>
<ul class="toc">
<li class="level3"><div class="li"><span class="li"><a href="#formatting_codes" class="toc">Formatting Codes</a></span></div></li>
<li class="level3"><div class="li"><span class="li"><a href="#formatting_modifiers" class="toc">Formatting Modifiers</a></span></div></li></ul>
</li></ul>
</li></ul>
</div>
</div>
<!-- TOC END -->



<h2><a name="printf" id="printf">printf</a></h2>
<div class="level2">

<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;">    <span class="co2">#include &lt;cstdio&gt;</span>
    <span class="kw4">int</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span> <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy0">*</span>format<span class="sy0">,</span> ... <span class="br0">&#41;</span><span class="sy0">;</span></pre>
<p>
The <code>printf</code> function prints output to <code>stdout</code>, according to <code>format</code> and other
arguments passed to <code>printf</code>. The string <code>format</code> consists of two types of items: 
characters that will be printed to the screen, and format commands that
define how the other arguments to <code>printf</code> are displayed. Basically, you
specify a format string that has text in it, as well as “special” characters
that map to the other arguments of <code>printf</code>. For example, this code
</p>
<pre class="c code c++" style="font-family:monospace;">     <span class="kw4">char</span> name<span class="br0">&#91;</span><span class="nu0">20</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="st0">&quot;Bob&quot;</span><span class="sy0">;</span>
     <span class="kw4">int</span> age <span class="sy0">=</span> <span class="nu0">21</span><span class="sy0">;</span>
     <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span> <span class="st0">&quot;Hello %s, you are %d years old<span class="es1">\n</span>&quot;</span><span class="sy0">,</span> name<span class="sy0">,</span> age <span class="br0">&#41;</span><span class="sy0">;</span></pre>
<p>
displays the following output:
</p>
<pre class="code">
     Hello Bob, you are 21 years old</pre>
<p>
The <code>%s</code> means, “insert the first argument, a string, right here.” The <code>%d</code>
indicates that the second argument (an integer) should be placed there. 
</p>

<p>
The return value of <code>printf</code> is the number of characters printed, or a negative
number if an error occurred.
</p>

</div>

<h3><a name="formatting_codes" id="formatting_codes">Formatting Codes</a></h3>
<div class="level3">

<p>

There are different %-codes for different variable types, as well as options to limit
the length of the variables and whatnot.

</p>
<table class="inline">
	<tr class="row0">
		<th class="col0">Code</th><th class="col1">Format</th>
	</tr>
	<tr class="row1">
		<td class="col0">%c</td><td class="col1">character</td>
	</tr>
	<tr class="row2">
		<td class="col0">%d</td><td class="col1">signed integers</td>
	</tr>
	<tr class="row3">
		<td class="col0">%i</td><td class="col1">signed integers</td>
	</tr>
	<tr class="row4">
		<td class="col0">%I64d</td><td class="col1">long long (8B integer), <acronym title="Microsoft">MS</acronym>-specific</td>
	</tr>
	<tr class="row5">
		<td class="col0">%I64u</td><td class="col1">unsigned long long (8B integer), <acronym title="Microsoft">MS</acronym>-specific</td>
	</tr>
	<tr class="row6">
		<td class="col0">%e</td><td class="col1">scientific notation, with a lowercase “e”</td>
	</tr>
	<tr class="row7">
		<td class="col0">%E</td><td class="col1">scientific notation, with a uppercase “E”</td>
	</tr>
	<tr class="row8">
		<td class="col0">%f</td><td class="col1">floating point</td>
	</tr>
	<tr class="row9">
		<td class="col0">%g</td><td class="col1">use %e or %f, whichever is shorter</td>
	</tr>
	<tr class="row10">
		<td class="col0">%G</td><td class="col1">use %E or %f, whichever is shorter</td>
	</tr>
	<tr class="row11">
		<td class="col0">%o</td><td class="col1">octal</td>
	</tr>
	<tr class="row12">
		<td class="col0">%s</td><td class="col1">a string of characters</td>
	</tr>
	<tr class="row13">
		<td class="col0">%u</td><td class="col1">unsigned integer</td>
	</tr>
	<tr class="row14">
		<td class="col0">%x</td><td class="col1">unsigned hexadecimal, with lowercase letters</td>
	</tr>
	<tr class="row15">
		<td class="col0">%X</td><td class="col1">unsigned hexadecimal, with uppercase letters</td>
	</tr>
	<tr class="row16">
		<td class="col0">%p</td><td class="col1">a pointer</td>
	</tr>
	<tr class="row17">
		<td class="col0">%n</td><td class="col1">the argument shall be a pointer to an integer into which is placed the number of characters written so far</td>
	</tr>
</table>

</div>

<h3><a name="formatting_modifiers" id="formatting_modifiers">Formatting Modifiers</a></h3>
<div class="level3">

<p>

An integer placed between a % sign and the format command acts as a minimum
field width specifier, and pads the output with spaces or zeros to make it long
enough. If you want to pad with zeros, place a zero before the minimum field
width specifier:
</p>
<pre class="c code c++" style="font-family:monospace;">     <span class="sy0">%</span>012d</pre>
<p>
You may also specify the minimum field width in an int variable if instead of a
number you put the * sign:
</p>
<pre class="c code c++" style="font-family:monospace;">     <span class="kw4">int</span> width <span class="sy0">=</span> <span class="nu0">12</span><span class="sy0">;</span>
     <span class="kw4">int</span> age <span class="sy0">=</span> <span class="nu0">100</span><span class="sy0">;</span>
     <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;%*d&quot;</span><span class="sy0">,</span> width<span class="sy0">,</span> age<span class="br0">&#41;</span><span class="sy0">;</span></pre>
<p>
You can also include a precision modifier, in the form of a <code>.N</code> where <code>N</code> is some
number, before the format command:
</p>
<pre class="c code c++" style="font-family:monospace;">     <span class="sy0">%</span>012.4d</pre>
<p>
The precision modifier has different meanings depending on the format command
being used:

</p>
<ul>
<li class="level1"><div class="li"> With <code>%e</code>, <code>%E</code>, and <code>%f</code>, the precision modifier lets you specify the number of decimal places desired. For example, <code>%12.6f</code> will display a floating number at least 12 digits wide, with six decimal places.</div>
</li>
<li class="level1"><div class="li"> With <code>%g</code> and <code>%G</code>, the precision modifier determines the maximum number of significant digits displayed.</div>
</li>
<li class="level1"><div class="li"> With <code>%s</code>, the precision modifier simply acts as a maximum field length, to complement the minimum field length that precedes the period.</div>
</li>
</ul>

<p>

As with field width specifier, you may use an int variable
to specify the precision modifier by using the * sign:
</p>
<pre class="c code c++" style="font-family:monospace;">     <span class="kw4">const</span> <span class="kw4">char</span><span class="sy0">*</span> msg <span class="sy0">=</span> <span class="st0">&quot;Hello printf&quot;</span><span class="sy0">;</span>
     <span class="kw4">int</span> string_size <span class="sy0">=</span> strlen <span class="br0">&#40;</span>msg<span class="br0">&#41;</span><span class="sy0">;</span>
     <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;msg: %.*s&quot;</span><span class="sy0">,</span> string_size<span class="sy0">,</span> msg<span class="br0">&#41;</span><span class="sy0">;</span></pre>
<p>
All of <code>printf</code>&#039;s output is right-justified, unless you place a minus sign
right after the % sign. For example,
</p>
<pre class="c code c++" style="font-family:monospace;">     <span class="sy0">%-</span><span class="nu17">12.4f</span></pre>
<p>
will display a floating point number with a minimum of 12 characters, 4 decimal
places, and left justified. 
</p>

<p>
You may modify the <code>%d</code>, <code>%i</code>, <code>%o</code>, <code>%u</code>, and <code>%x</code> type
specifiers with the letter <code>l</code> and the letter <code>h</code> to specify <code>long</code> and <code>short</code> data
types (e.g. <code>%hd</code> means a <code>short</code> integer). 
</p>

<p>
The <code>%e</code>, <code>%f</code>, and <code>%g</code> type specifiers can have the letter <code>l</code> before them to indicate that a double follows. 
The <code>%g</code>, <code>%f</code>, and <code>%e</code> type specifiers can be preceded with the character <code>#</code> to ensure that
the decimal point will be present, even if there are no decimal digits. 
</p>

<p>
The use of the <code>#</code> character with the <code>%x</code> type specifier indicates that the hexidecimal
number should be printed with the <code>0x</code> prefix. 
</p>

<p>
The use of the <code>#</code> character
with the <code>%o</code> type specifier indicates that the octal value should be displayed
with a <code>0</code> prefix.
</p>

<p>
Inserting a plus sign <code>+</code> into the type specifier will force positive values to
be preceded by a <code>+</code> sign. Putting a space character &#039; &#039; there will force
positive values to be preceded by a single space character.
</p>

<p>
You can also include <a href="../../escape_sequences.html" class="wikilink1" title="escape_sequences">constant escape sequences</a> in the output string.
</p>

<p>
Related Topics: <a href="../../c/io/fprintf.html" class="wikilink1" title="c:io:fprintf">fprintf</a>, <a href="../../c/io/puts.html" class="wikilink1" title="c:io:puts">puts</a>, <a href="../../c/io/scanf.html" class="wikilink1" title="c:io:scanf">scanf</a>, <a href="../../c/io/sprintf.html" class="wikilink1" title="c:io:sprintf">sprintf</a>
</p>

</div>

    <!-- wikipage stop -->
  </div>

  <div class="clearer">&nbsp;</div>

  
  <div class="stylefoot">

    <div class="meta">
      <div class="user">
              </div>
      <!--
      <div class="doc">
        c/io/printf.txt &middot; Last modified: 04/08/2010 07:16 by nate      </div>
      -->
    </div>

   
    </div></div></body>
</html>