{"id":6885,"date":"2025-03-29T00:01:53","date_gmt":"2025-03-29T07:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6885"},"modified":"2025-04-05T08:50:02","modified_gmt":"2025-04-05T15:50:02","slug":"the-phone-number-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6885","title":{"rendered":"The Phone Number String"},"content":{"rendered":"<p>This Lesson&#8217;s topic is more about database programming than C programming, but the philosophy still applies. When do you store a number as a value as opposed to storing it as a string? Two famous examples are zip codes and phone numbers.<br \/>\n<!--more--><br \/>\nI suppose the answer depends on what you plan on doing with the number. For example, if a zip code needs to be part of a mathematical expression, assigning it as a value makes sense. Sorting, of course, works with both strings and values. When storage is an issue, values occupy less space than strings.<\/p>\n<p>For practice, assume the task is to format a phone number. The point isn&#8217;t to sort the number or to process it through the phone system, but just to present what starts as a value as a formatted string. Specifically, take the <em>long int<\/em> value <code>5551234001 <\/code>and output it as the string <code>(555) 123-4001<\/code>.<\/p>\n<p>I can think of two ways to accomplish this task in C. The first and easiest is to convert the <em>long int<\/em> value into a string. The string is then output one character at a time, interspliced with the proper phone number formatting characters as presented in the United States. Here is the string conversion code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_03_29-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_03_29-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    <span class=\"comments\">\/* phone numbers for this example have 10 digits *\/<\/span>\r\n    const int digits = 10;\r\n    const int open_paren = 0;\r\n    const int close_paren = 3;\r\n    const int dash = 6;\r\n    long phone;\r\n    char number[digits+1];\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* assign phone number as long value *\/<\/span>\r\n    phone = 5551234001;\r\n\r\n    <span class=\"comments\">\/* convert to a string *\/<\/span>\r\n    snprintf(number,digits+1,\"%ld\",phone);\r\n\r\n    <span class=\"comments\">\/* process the phone number *\/<\/span>\r\n    for( x=0; x&lt;digits; x++ )\r\n    {\r\n        switch(x)\r\n        {\r\n            case open_paren:\r\n                printf(\"(%c\",number[x]);\r\n                break;\r\n            case close_paren:\r\n                printf(\") %c\",number[x]);\r\n                break;\r\n            case dash:\r\n                printf(\"-%c\",number[x]);\r\n                break;\r\n            default:\r\n                printf(\"%c\",number[x]);\r\n        }\r\n    }\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>snprintf()<\/em> function converts the <em>long int<\/em> value stored in variable <code>phone<\/code> to a string, stored in <em>char<\/em> array <code>buffer[]<\/code>:<\/p>\n<p><code>snprintf(number,digits+1,\"%ld\",phone);<\/code><\/p>\n<p>Constant integer <code>digits<\/code> stores the phone number&#8217;s length, 10 characters. But to assign storage for the string, one extra byte is required for the null character: <code>char number[digits+1]<\/code> This value is also used in the <em>snprintf()<\/em> statement to ensure that the entire string fits in <code>buffer[]<\/code>, <code>digits+1<\/code>. (When you forget to add one, the output is truncated.)<\/p>\n<p>A <em>for<\/em> loop outputs characters in <code>buffer[]<\/code> one at a time. A <em>switch-case<\/em> structure monitors the offsets, outputting formatting characters at their proper locations in the string. These positions are set by using constants: <code>open_paren<\/code>, <code>close_paren<\/code>, and <code>dash<\/code>.<\/p>\n<p>Here is the output:<\/p>\n<p><code>(555) 123-4001<\/code><\/p>\n<p>The string approach is easy because strings are naturally processed from left-to-right. I don&#8217;t know whether this simplicity of processing means that it&#8217;s best to store a phone number as a string. Such a decision is still up to whomever is coding the database, but it&#8217;s certain uncomplicated from my perspective.<\/p>\n<p>What&#8217;s more difficult is processing output when the phone number is stored as a value. This task requires some additional thinking plus a sprinkling of math, but it still works sorta the same. I present a solution in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6896\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes numeric data shouldn&#8217;t be stored numerically. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6885\">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-6885","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\/6885","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=6885"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6885\/revisions"}],"predecessor-version":[{"id":6920,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6885\/revisions\/6920"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}