{"id":4887,"date":"2021-07-31T00:01:35","date_gmt":"2021-07-31T07:01:35","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4887"},"modified":"2021-07-24T09:07:09","modified_gmt":"2021-07-24T16:07:09","slug":"reading-strings-with-the-sscanf-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4887","title":{"rendered":"Reading strings with the <em>sscanf()<\/em> Function"},"content":{"rendered":"<p>I&#8217;m not a fan of the <em>scanf()<\/em> function. It&#8217;s an input function, quick enough to toss out there for a beginner to write a (somewhat) interactive programs. But the function itself is horrid, with complex arguments and dubious results.<\/p>\n<p>So imagine my delight at finding its companion function, <em>sscanf()<\/em>.<br \/>\n<!--more--><br \/>\nThe <em>sscanf()<\/em> function works like <em>scanf()<\/em>, though it reads its input from a string (<em>char<\/em> array) and not standard input, hence the extra &#8220;s&#8221; in the name. Just like <em>scanf()<\/em>, the <em>sscanf()<\/em> function is touchy. Here&#8217;s the <em>man<\/em> page format:<\/p>\n<p><code>int sscanf(const char *restrict s, const char *restrict format, ...);<\/code><\/p>\n<p>The first argument is a string, <code>s<\/code>, which is scanned for the data as opposed to standard input. The rest of the arguments are the same as for <em>scanf()<\/em>: a formatting string and variables (pointers).<\/p>\n<p>The <em>sscanf()<\/em> function works brilliantly &mdash; providing that the input string is clean, specifying the exact data type requested by the format string. The following code demonstrates.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_07_31-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_07_31-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 value[] = \"827\";\r\n    int cost;\r\n\r\n    sscanf(value,\"%d\",&amp;cost);\r\n    printf(\"The dress cost $%d\\n\",cost);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The string <code>value<\/code> is processed at Line 8. It contains the characters <code>827<\/code>, which are converted into integer value 827 by <em>sscanf()<\/em> and stored in variable <code>cost<\/code>:<\/p>\n<p><code>The dress cost $827<\/code><\/p>\n<p>As with <em>scanf()<\/em>, the <em>sscanf()<\/em> function has numerous quirks that limit its capability to magically extract data from a string. For example, say you want to ensure that the dollar sign is set in the input string. If so, you use this <em>sscanf()<\/em> function format string:<\/p>\n<p><code>sscanf(value,\"$%d\",&amp;cost);<\/code><\/p>\n<p>The <code>\"$%d\"<\/code> means the string is scanned for a dollar sign followed immediately by a number.<\/p>\n<p><code>char value[] = \"$827\";<\/code><\/p>\n<p>Given the above string, the function successfully extracts the value. But if that dollar sign is missing, no value is read and variable <code>cost<\/code> is undefined. You can prevent such a thing from happening by examining the return value from <em>sscanf()<\/em>. If it&#8217;s non-zero, then a value was successfully read, as shown in the following code.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_07_31-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_07_31-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 value[] = \"827\";\r\n    int cost,r;\r\n\r\n    r = sscanf(value,\"$%d\",&amp;cost);\r\n    if( r!=0 )\r\n        printf(\"The dress cost $%d\\n\",cost);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This program generates no output because the <code>$<\/code> character required by the <em>sscanf()<\/em> format string is missing in the <code>value<\/code> string.<\/p>\n<p>Alas, you can&#8217;t use <em>sscanf()<\/em> to see whether a value exists within a string. As with <em>scanf()<\/em>, the <em>sscanf()<\/em> function stops reading when a whitespace character is encountered. Beyond that first whitespace character, the rest of the string doesn&#8217;t exist as far as <em>sscanf()<\/em> is concerned.<\/p>\n<p>I can imagine a few instances where my code could use the <em>sscanf()<\/em> function, but only when I&#8217;m certain that the string being examined is in a specific format. Even then, I&#8217;d probably write my own extraction\/parsing function just because the <em>scanf()<\/em> family of functions is so prissy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just as the horrible <em>scanf()<\/em> function reads from standard input, the dreadful <em>sscanf()<\/em> function reads input from strings. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4887\">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-4887","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\/4887","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=4887"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4887\/revisions"}],"predecessor-version":[{"id":4903,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4887\/revisions\/4903"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}