{"id":288,"date":"2013-09-21T00:01:40","date_gmt":"2013-09-21T08:01:40","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=288"},"modified":"2017-01-21T08:56:24","modified_gmt":"2017-01-21T16:56:24","slug":"conversion-character-mania-integer-output","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=288","title":{"rendered":"Conversion Character Mania: Integer Output"},"content":{"rendered":"<p>The <em>printf()<\/em> function&#8217;s power lies in its formatting abilities, specifically the display of values. That power is vast, but the documentation showing how it works really sucks.<br \/>\n<!--more--><br \/>\nRather than try to create this massive grid that shows all the <em>printf()<\/em> conversion character options and settings, I thought I&#8217;d concentrate on presenting each conversion character based on the type of value it formats. This week&#8217;s lesson is about the integer output conversion characters.<\/p>\n<p>Because integer values can be either <em>signed<\/em> or <em>unsigned<\/em>, C uses two different conversion characters to format output: <code>%d<\/code> for <em>signed int<\/em> values and <code>%u<\/code> for <em>unsigned<\/em>.<\/p>\n<p>The <code>%d<\/code> and <code>%u<\/code> conversion characters handle the variable types shown in Table 1.<\/p>\n<div align=\"center\">\n<table border=\"1\" cellspacing=\"2\">\n<tr>\n<td>&nbsp;<\/td>\n<td align=\"center\"><code>%d<\/code><\/td>\n<td align=\"center\"><code>%u<\/code><\/td>\n<\/tr>\n<tr>\n<td><em>int<\/em><\/td>\n<td align=\"center\">X<\/td>\n<td align=\"center\">&nbsp;<\/td>\n<\/tr>\n<tr>\n<td><em>short<\/em><\/td>\n<td align=\"center\">X<\/td>\n<td align=\"center\">&nbsp;<\/td>\n<\/tr>\n<tr>\n<td><em>signed int<\/em><\/td>\n<td align=\"center\">X<\/td>\n<td align=\"center\">&nbsp;<\/td>\n<\/tr>\n<tr>\n<td><em>unsigned<\/em><\/td>\n<td align=\"center\">&nbsp;<\/td>\n<td align=\"center\">X<\/td>\n<\/tr>\n<tr>\n<td><em>unsigned short<\/em><\/td>\n<td align=\"center\">&nbsp;<\/td>\n<td align=\"center\">X<\/td>\n<\/tr>\n<tr>\n<td><em>unsigned int<\/em><\/td>\n<td align=\"center\">&nbsp;<\/td>\n<td align=\"center\">X<\/td>\n<\/tr>\n<tr>\n<td><em>char<\/em><\/td>\n<td align=\"center\">X<\/td>\n<td align=\"center\">&nbsp;<\/td>\n<\/tr>\n<tr>\n<td><em>unsigned char<\/em><\/td>\n<td align=\"center\">&nbsp;<\/td>\n<td align=\"center\">X<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>The same conversion characters are used for <em>long int<\/em> values as well. In that case you prefix the <code>d<\/code> or <code>u<\/code> with an <code>l<\/code> (lower case L): <code>%ld<\/code> and <code>%lu<\/code><\/p>\n<p>The <em>double long<\/em> or <em>long long<\/em> integer values use two Ls: <code>%lld<\/code> and <code>%llu<\/code><\/p>\n<p>You can sandwich a width value between the percent sign and the conversion character letters. This value specifies how many characters wide to format the output. The purpose of the width value is to help align output to a fixed field.<\/p>\n<p>In the following code, the value 8 is used to set the output to 8 characters.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int i = 498;\r\n\r\n    printf(\"Value:%8d\\n\",i);\r\n    printf(\"Value:%8d\\n\",i*10);\r\n    printf(\"Value:%8d\\n\",i*100);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The integer is right-aligned in the output, which looks like this:<\/p>\n<pre><code>Value:     498\r\nValue:    4980\r\nValue:   49800<\/code><\/pre>\n<p>The width value can apparently be any size; I&#8217;ve tested it up to 4,300,000,000 characters wide and the compiler didn&#8217;t puke.<\/p>\n<p>If, for some bizarre reason, you wanted to left-justify the values, change the placeholder to <code>%-8d<\/code> in each <em>printf()<\/em> statement. Here&#8217;s the output:<\/p>\n<p><code>Value:498<br \/>\nValue:4980<br \/>\nValue:49800<\/code><\/p>\n<p>You don&#8217;t see it, but spaces are output to the right of the numbers above.<\/p>\n<p>Another prefix you can toss into integer output is <code>0<\/code>. What it does is to prefix output with zeros to pad the specified width. The <code>0<\/code> has to appear before the width value.<\/p>\n<p>For example, if you change all the <code>%8d<\/code> conversion characters in the above code to <code>%08d<\/code>, the output looks like this:<\/p>\n<p><code>Value:00000498<br \/>\nValue:00004980<br \/>\nValue:00049800<\/code><\/p>\n<p>The output is still 8 characters wide, but zeros pad each value on the left.<\/p>\n<p>In case you&#8217;re curious, you can&#8217;t use both <code>-<\/code> and <code>0<\/code> together. If you want to pad zeros on the right side of a number, you&#8217;ll need to concoct your own function.<\/p>\n<p>To prefix a sign (+ or -) to the output, place a <code>+<\/code> character right after the <code>%<\/code> symbol. That character is inserted along with other options except the <code>0<\/code> prefix. All of these formats are valid:<\/p>\n<p><code>printf(\"Value:%8d\\n\",678);<br \/>\nprintf(\"Value:%08d\\n\",678);<br \/>\nprintf(\"Value:%+8d\\n\",678);<br \/>\nprintf(\"Value:%-8d\\n\",678);<br \/>\nprintf(\"Value:%+-8d\\n\",678);<\/code><\/p>\n<p>Here&#8217;s the output for those five statements:<\/p>\n<pre><code>Value:     678\r\nValue:00000678\r\nValue:    +678\r\nValue:678     \r\nValue:+678  <\/code><\/pre>\n<p>In case you&#8217;re curious, you cannot prefix a currency symbol to the output by using a conversion character. Something like <code>%$d<\/code> or <code>%$8d<\/code> isn&#8217;t valid, although it might be in other programming languages.<\/p>\n<p>And now, the stragglers.<\/p>\n<p>Some integer conversion characters linger from the early days of C, although they&#8217;re pretty much unused these days. For example, the <code>%i<\/code> conversion character displays integer values, but I recommend using <code>%d<\/code> instead.<\/p>\n<p>The <code>h<\/code> prefix can be used like <code>l<\/code>, but to display <em>short int<\/em> values. For example, <code>%hd<\/code> ensures that a <em>short int<\/em> value is output. I believe the <code>h<\/code> stands for &#8220;half&#8221; as in half an <em>int<\/em> value. (Internally, that means the variable would be represented by two bytes instead of four.)<\/p>\n<p>I&#8217;ll cover conversion character options for displaying floating point values in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=299\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explore the various options available for displaying integer values by using <em>printf()<\/em>&#8216;s integer output conversion characters. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=288\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-288","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=288"}],"version-history":[{"count":15,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/288\/revisions"}],"predecessor-version":[{"id":2339,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/288\/revisions\/2339"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}