This Lesson’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.
I 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.
For practice, assume the task is to format a phone number. The point isn’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 long int value 5551234001
and output it as the string (555) 123-4001
.
I can think of two ways to accomplish this task in C. The first and easiest is to convert the long int 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:
2025_03_29-Lesson.c
#include <stdio.h> int main() { /* phone numbers for this example have 10 digits */ const int digits = 10; const int open_paren = 0; const int close_paren = 3; const int dash = 6; long phone; char number[digits+1]; int x; /* assign phone number as long value */ phone = 5551234001; /* convert to a string */ snprintf(number,digits+1,"%ld",phone); /* process the phone number */ for( x=0; x<digits; x++ ) { switch(x) { case open_paren: printf("(%c",number[x]); break; case close_paren: printf(") %c",number[x]); break; case dash: printf("-%c",number[x]); break; default: printf("%c",number[x]); } } putchar('\n'); return 0; }
The snprintf() function converts the long int value stored in variable phone
to a string, stored in char array buffer[]
:
snprintf(number,digits+1,"%ld",phone);
Constant integer digits
stores the phone number’s length, 10 characters. But to assign storage for the string, one extra byte is required for the null character: char number[digits+1]
This value is also used in the snprintf() statement to ensure that the entire string fits in buffer[]
, digits+1
. (When you forget to add one, the output is truncated.)
A for loop outputs characters in buffer[]
one at a time. A switch-case structure monitors the offsets, outputting formatting characters at their proper locations in the string. These positions are set by using constants: open_paren
, close_paren
, and dash
.
Here is the output:
(555) 123-4001
The string approach is easy because strings are naturally processed from left-to-right. I don’t know whether this simplicity of processing means that it’s best to store a phone number as a string. Such a decision is still up to whomever is coding the database, but it’s certain uncomplicated from my perspective.
What’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 next week’s Lesson.