{"id":327,"date":"2013-10-12T00:01:04","date_gmt":"2013-10-12T08:01:04","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=327"},"modified":"2013-10-03T09:51:31","modified_gmt":"2013-10-03T17:51:31","slug":"conversion-character-mania-the-rest","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=327","title":{"rendered":"Conversion Character Mania: The Rest"},"content":{"rendered":"<p>I don&#8217;t think I&#8217;ve ever seen the full lot of <em>printf()<\/em> conversion characters explained in any detail. This lesson is the last in a series of my attempt to do just that.<br \/>\n<!--more--><br \/>\nFirst, a recap. Here are all the major <em>printf()<\/em> conversion characters:<\/p>\n<table>\n<tr>\n<td width=\"20%\"><code>d, i<\/code><\/td>\n<td>Integer values output as a decimal number<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>u<\/code><\/td>\n<td>Unsigned integer values output as a decimal number<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>f<\/code><\/td>\n<td>Floating point (real) values, <em>float<\/em> or <em>double<\/em><\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>e, E<\/code><\/td>\n<td>Floating point values output using scientific notation<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>g, G<\/code><\/td>\n<td>Floating point values output using scientific notation if the value is huge or as a real value if it&#8217;s small<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>c<\/code><\/td>\n<td>Character values output as a single character (text)<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>s<\/code><\/td>\n<td>Strings output as text<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>x, X<\/code><\/td>\n<td>Integer values output as hexadecimal, upper or lower case letters<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>o<\/code><\/td>\n<td>Integer values output as octal<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>p<\/code><\/td>\n<td>Memory address (pointer variable) output usually in hexadecimal<\/td>\n<\/tr>\n<tr>\n<td width=\"20%\"><code>%<\/code><\/td>\n<td>Used to output a %<\/td>\n<\/tr>\n<\/table>\n<p>Previous lessons have covered most of these conversion characters. What remains are <code>%x<\/code>\/<code>%X<\/code>, <code>%o<\/code>, <code>%p<\/code>, and <code>%%<\/code>.<\/p>\n<p>First the easy one: <code>%%<\/code> outputs a single percent sign, <code>%<\/code>. Because the % is used as the conversion character prefix, <code>%%<\/code> is required whenever you need to output a % by using the <em>printf()<\/em> function. Remember that or a compiler warning will remind you.<\/p>\n<p>The <code>%x<\/code> and <code>%X<\/code> conversion characters output an integer value in hexadecimal. When <code>%x<\/code> is used, hex numbers A through F are displayed in lower case; <code>%X<\/code> outputs them in upper case.<\/p>\n<p>The <code>%o<\/code> conversion character outputs integer values using octal or base eight. That was very popular when the C language was developed back in the 1970s, but not so popular today. In fact, I&#8217;m pretty offended by octal these days.<\/p>\n<p>Like the other integer output functions, you can add a width option and <code>-<\/code> for justification with <code>%x<\/code>\/<code>%X<\/code> and <code>%c<\/code>. A leading zero on the width option pads the left side of output with zeros. Here&#8217;s your sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt&stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int value;\r\n\r\n    printf(\"Type an integer: \");\r\n    scanf(\"%d\",&value);\r\n\r\n    printf(\"The value: %8d\\n\",value);\r\n    printf(\"With %%X:   %8X\\n\",value);\r\n    printf(\"With %%x:   %8x\\n\",value);\r\n    printf(\"With %%o:   %8o\\n\",value);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Sample run:<\/p>\n<pre><code>Type an integer: <strong>54321<\/strong>\r\nThe value:    54321\r\nWith %X:       D431\r\nWith %x:       d431\r\nWith %o:     152061<\/code><\/pre>\n<p>The <code>%p<\/code> conversion character displays the address (memory location) of a pointer variable, or of any variable prefixed by the <code>&<\/code> operator. The output is often in hexadecimal, though the format for that number depends on the compiler or computing platform.<\/p>\n<p>I typically use <code>%p<\/code> for debugging purposes, to discover a memory location or to demonstrate how pointers work. Beyond that it&#8217;s not too popular.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int array[3] = { 397, 457, 521 };\r\n    int x;\r\n\r\n    for(x=0;x<3;x++)\r\n        printf(\"%d: %p %d\\n\",\r\n            x+1,\r\n            &#038;array[x],\r\n            array[x]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Sample run:<\/p>\n<pre><code>1: 0x7fff4fc37abc 397\r\n2: 0x7fff4fc37ac0 457\r\n3: 0x7fff4fc37ac4 521<\/code><\/pre>\n<p>The memory locations generated by <code>%p<\/code> will be different on your computer, and may even carry a different format.<\/p>\n<p>Your compiler may also support non-standard conversion characters. You need to check the <em>printf()<\/em> man page for their availability. There's no guarantee, however, that those specific options work or will even compile on other computing platforms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The final assortment of <em>printf()<\/em> conversion characters are covered, wrapping up my Conversion Character Mania series. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=327\">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-327","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\/327","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=327"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/327\/revisions"}],"predecessor-version":[{"id":350,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/327\/revisions\/350"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}