{"id":5002,"date":"2021-10-09T00:01:21","date_gmt":"2021-10-09T07:01:21","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5002"},"modified":"2021-10-02T09:37:52","modified_gmt":"2021-10-02T16:37:52","slug":"misused-placeholders","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5002","title":{"rendered":"Misused Placeholders"},"content":{"rendered":"<p>I received a question from a reader about improperly specifying a <em>printf()<\/em> placeholder. Specifically, he used <code>%d<\/code> (decimal integer) to output a string. Most compilers flag this condition as a warning, mismatched types or something similar. Still, the program is created and it runs. What does the output mean?<br \/>\n<!--more--><br \/>\nHere is the statement:<\/p>\n<p><code>printf(\"%d\\n\",string);<\/code><\/p>\n<p>Variable <code>string<\/code> is a <em>char<\/em> array. The compiler dislikes the mismatch of <code>%d<\/code> to a string (<em>char<\/em> array). The warning generated looks something like this:<\/p>\n<p><code>warning: format specifies type 'unsigned int' but the argument has type 'char *' [-Wformat]<\/code><\/p>\n<p>Still, the program builds &#8211; and runs. Here&#8217;s the full code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_10_09-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_10_09-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"ABCD\";\r\n\r\n    printf(\"%s\\n\",string);\r\n    printf(\"%d\\n\",string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>And here is the output:<\/p>\n<p><code>ABCD<br \/>\n-324285993<\/code><\/p>\n<p>I find the value -324285993 curious. What could it be? This question is what the reader raised: Is the numeric output the integer representation of the string? Could it be the string&#8217;s address? Some sleuthing is required.<\/p>\n<p>The first change I made to the code is to replace the <code>%d<\/code> placeholder with <code>%X<\/code> for hexadecimal integer output. This way I could see if any patterns existed in the integer value, specifically if it&#8217;s a numeric representation of the string itself. Here&#8217;s the program&#8217;s output after making this change:<\/p>\n<p><code>ABCD<br \/>\nE17849D7<\/code><\/p>\n<p>Still no help. If the integer represents the string, it would contain hex values 41, 42, 43, and 44 for letters ABCD.<\/p>\n<p>To continue to resolve the mystery, the next thing to test for is whether the integer value represents the string&#8217;s location in memory &mdash; a pointer. To confirm this guess, I added a third <em>printf()<\/em> statement to the code, as shown here:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_10_09-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_10_09-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"ABCD\";\r\n\r\n    printf(\"%s\\n\",string);\r\n    printf(\"%X\\n\",string);\r\n    printf(\"%p\\n\",string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 9, the <code>%p<\/code> placeholder outputs the address of variable <code>string<\/code>. If this value is the same as the value output with the <code>%X<\/code> placeholder, mystery solved. Here is the updated program&#8217;s output:<\/p>\n<p><code>ABCD<br \/>\nE9D3E9D7<br \/>\n0x7ffee9d3e9d7<\/code><\/p>\n<p>The third line of output isn&#8217;t identical to the second line&#8217;s value, though the last eight digits match: <code>E9D3E9D7<\/code>. The value the <code>%d<\/code> placeholder outputs is the variable&#8217;s address, but truncated to fit into an integer&#8217;s bit width.<\/p>\n<p>My guess is that the <code>%d<\/code> placeholder interprets the value presented by variable <code>string<\/code> as an address, not as the string&#8217;s contents. This guess makes sense if you consider the the <code>%s<\/code> placeholder uses the string&#8217;s address to start popping out characters. Processing text isn&#8217;t the job of a <code>%d<\/code> placeholder, or even the <code>%p<\/code> placeholder. So it does the best it can, interpreting the value presented.<\/p>\n<p>To get back to the reader&#8217;s question, the reason a warning is generated and not an error is that the mistake is really on behalf of the programmer. The code builds, but the output is suspect. In this example, the output is questionable in that the integer represents only a portion of the string&#8217;s address &mdash; if such output is what the programmer desired in the first place.<\/p>\n<p>As I&#8217;ve repeated many times, take compiler warnings seriously. Your code may build with warnings intact, but with only a handful of exceptions, you should fix the warning and write better code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The compiler may flag the blunder, but output is still generated. What does it mean? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5002\">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-5002","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\/5002","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=5002"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5002\/revisions"}],"predecessor-version":[{"id":5009,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5002\/revisions\/5009"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}