{"id":3662,"date":"2019-07-06T00:01:52","date_gmt":"2019-07-06T07:01:52","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3662"},"modified":"2019-07-13T09:46:31","modified_gmt":"2019-07-13T16:46:31","slug":"peculiarities-of-the-strtol-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3662","title":{"rendered":"Peculiarities of the <em>strtol()<\/em> Function"},"content":{"rendered":"<p>Imagine my disappointment to learn that the <em>atoi()<\/em> function, one of the first I learned in C, is actually a macro. I&#8217;m horrified. As much as I treasure converting strings to integers, I shall survive this life-changing discovery.<br \/>\n<!--more--><br \/>\nThe <em>strtol()<\/em> function most likely stands for string-to-long It&#8217;s prototyped in the <code>stdlib.h<\/code> header file. Here&#8217;s the man page format:<\/p>\n<p><code>long strtol(const char *restrict str, char **restrict endptr, int base);<\/code><\/p>\n<p>The <code>str<\/code> argument is the string containing the number to convert. The <code>endptr<\/code> argument helps determine whether the <code>str<\/code> string contained zero or was empty. And <code>base<\/code> sets the way the input value is interpreted as base 10, 8, or 16.<\/p>\n<p>Here&#8217;s some sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char x[] = \"123456\";\r\n    char y[] = \"003224\";\r\n    char z[] = \"0xA72B\";\r\n    int a,b,c;\r\n\r\n    a = strtol( x, NULL, 10);\r\n    b = strtol( y, NULL, 8);\r\n    c = strtol( z, NULL, 16);\r\n\r\n    printf(\"%s in base 10: %d\\n\",x,a);\r\n    printf(\"%s in base 8: %o\\n\",y,b);\r\n    printf(\"%s in base 16: %x\\n\",z,c);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The three string values in arrays <code>x<\/code>, <code>y<\/code>, and <code>z<\/code> are expressed in bases 10, 8, and 16, respectively. The three <em>strtol()<\/em> functions convert each value; the <code>NULL<\/code> pointer means that the <code>endptr<\/code> argument is ignored. And the values returned are displayed in their proper formats. Here&#8217;s the output:<\/p>\n<p><code>123456 in base 10: 123456<br \/>\n003224 in base 8: 3224<br \/>\n0xA72B in base 16: a72b<\/code><\/p>\n<p>For most purposes, the <code>strtol( x, NULL, 10)<\/code> format of the statement is used, which converts the string referenced by variable <code>x<\/code> into a decimal value. In fact, this is the <em>atoi()<\/em> macro defined in the stdlib.h header file:<\/p>\n<p><code>(int)strtol(str, (char **)NULL, 10);<\/code><\/p>\n<p>Sneaky.<\/p>\n<p>The <em>strtol()<\/em> function is far more powerful than that old <em>atoi()<\/em> function, which makes it even more valuable and definitely the conversion function you want to use for potentially pesky input values. I expand on some if its input-error detection techniques in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3671\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When converting a string to an integer, the <em>strtol()<\/em> function provides a few interesting options. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3662\">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-3662","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\/3662","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=3662"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3662\/revisions"}],"predecessor-version":[{"id":3684,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3662\/revisions\/3684"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}