{"id":6896,"date":"2025-04-05T00:01:37","date_gmt":"2025-04-05T07:01:37","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6896"},"modified":"2025-03-29T09:35:33","modified_gmt":"2025-03-29T16:35:33","slug":"the-phone-number-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6896","title":{"rendered":"The Phone Number Value"},"content":{"rendered":"<p>As a string, a phone number is easy to parse, left-to-right, as shown in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6885\">last week&#8217;s Lesson<\/a>. As a value, however, the processing requires mathematical manipulation to work it from left-to-right.<br \/>\n<!--more--><br \/>\nWhile both numbers and values are read from left-to-right (in English), it&#8217;s easier to process a number from right-to-left. The reason is that numbers can be any length, yet their first digit is always on the far left. Even when a you (a human, I&#8217;m supposing) reads a number, you confirm its length before you start reading. This is how you know whether to start in the millions, thousands, or hundreds.<\/p>\n<p>An advantage for a US phone number is that a programmer knows that it&#8217;s ten digits long. To process a ten-digit value, you must discover the 10th digit while ignoring the next nine.<\/p>\n<p>For example, consider the phone number (555) 123-4001 presented in last week&#8217;s code. As a value, you must get from 5,551,234,001 to 5,000,000,000. Then you must divide the result by 1,000,000,000 to obtain 5, the first digit in the phone number. This process repeats for the next digit, using 100,000,000 instead of 1,000,000,000 to manipulate the original value.<\/p>\n<p>The following code demonstrates this process. The core of the code is based on the string version presented in last week&#8217;s Lesson. The constant values are adjusted to account for the way the digits are processed.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_04_05-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_04_05-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    <span class=\"comments\">\/* phone numbers for this example have ten digits *\/<\/span>\r\n    const int digits = 10;\r\n    const int open_paren = 10;\r\n    const int close_paren = 7;\r\n    const int dash = 4;\r\n    long phone,p;\r\n    int x,d;\r\n\r\n    <span class=\"comments\">\/* assign phone number as a long value *\/<\/span>\r\n    phone = 5551234001;\r\n\r\n    <span class=\"comments\">\/* process the phone number *\/<\/span>\r\n    for( x=digits; x&gt;0; x-- )\r\n    {\r\n        <span class=\"comments\">\/* extract each digit, left-to-right *\/<\/span>\r\n        p = (long)pow(10,x);\r\n        d = (phone % p)\/(p\/10);\r\n\r\n        <span class=\"comments\">\/* output the formatting phone number *\/<\/span>\r\n        switch(x)\r\n        {\r\n            case open_paren:\r\n                printf(\"(%d\",d);\r\n                break;\r\n            case close_paren:\r\n                printf(\") %d\",d);\r\n                break;\r\n            case dash:\r\n                printf(\"-%d\",d);\r\n                break;\r\n            default:\r\n                printf(\"%d\",d);\r\n        }\r\n    }\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>A <em>for<\/em> loop works through the phone number&#8217;s length, counting from 10 to 1. For each value of <code>digit<\/code>, its corresponding power of 10 is calculated, <code>(long)pow(10,x)<\/code>, with the result stored in variable <code>p<\/code>. This step makes the next calculation (the following statement) more compact.<\/p>\n<p>To obtain the digit, the modulus of the phone value and the power of 10 is calculated, then divided by the power of 10: <code>(phone % p)\/(p\/10)<\/code>. The result is the single digit, stored in variable <code>d<\/code>.<\/p>\n<p>Yes, it took me time to figure out how to perform the math magic in this code. The part I missed was the <code>p\/10<\/code>.<\/p>\n<p>Once the single digit is obtained, the <em>switch-case<\/em> structure outputs the phone number in the US phone number format, similar to what was presented in last week&#8217;s Code.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>(555) 123-4001<\/code><\/p>\n<p>Remember that to build this code in Linux, you must include the math library; use the <code>-lm<\/code> switch at the command prompt.<\/p>\n<p>I&#8217;m certain other approaches are possible to obtain each digit from a value, left-to-right. In fact, it would be fun to work the number from right-to-left and then figure out how to output the values backwards. I may present this code sometime in the future.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Storing numeric data as a number involves a lot more brainwork than storing it as a string. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6896\">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-6896","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\/6896","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=6896"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6896\/revisions"}],"predecessor-version":[{"id":6908,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6896\/revisions\/6908"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}