Printing integers with spaces between the digits Problem:  Given an integer between 1 and 32767 print the individual digits with 2 spaces between them.  

I am using C How To Program but this is not homework.  The book has not gotten to arrays or anything more complicated than looping structures and function. I try to avoid using those structures and concepts that have not been introduced yet.  I get the digits to separate and output.  I was wondering if there is a better way to do this using the tools I have.  

My Code:

//-----------------------------------------------------------------------------
// Program: EX5_22.c - Separating Digits
// Programmer:  Joseph Cunningham
// Class: CsC_20 - c
// Date: 2/16/19
//
// This program will prompt the user for a number from 1 - 32767.  It will then
// output that number separated into its constituent digits with 2 spaces
// between each digit
//-----------------------------------------------------------------------------

#include&lt;stdio.h&gt;
#include&lt;math.h&gt;

int getNumLength(int number);
void separate(int number, int power); // Fucntion prototype

int main (void)
{
  // Variable declaration

  int number; // User entered number to digitize
  int power;  // Power to generate divisor

  // Prompt user for number

  printf("Please input a number (1-32767): ");
  scanf("%d", &amp;number);

  // Get the power for the divisor

  power = getNumLength(number);

  // Output digits

  separate(number, power);

  return 0;
}

//-----------------------------------------------------------------------------
// Function: getPower(number) - counts the number of digits in the number to
// generate a power
// Input:  int number - the user entered number
// Output: none
// Return value - length of thr number
//-----------------------------------------------------------------------------

int getNumLength(int number)
{
  int length; // the length of number

  while (number != 0)
  {
    number = number / 10;
    length++;
  }

  return length;
}

//-----------------------------------------------------------------------------
// Function: separate(number) - separates and displays an integer between 1
// -32767 into its digits
// Input: int number - intger betwenn 1 - 32767, int power - power for divisor
// Output: numbers digits separated by 2 spaces
// Return value: none
//-----------------------------------------------------------------------------

void separate(int number, int power)
{
  int divisor; // the divisor from power
  int i;  // loop counter

  for(i = power - 1; i &gt;=0; i--)
  {
    divisor = (int)pow(10, i);
    printf("%d  ", number / divisor);
    number = number % divisor;
  }
}
// Variable declaration

  int number; // User entered number to digitize
  int power;  // Power to generate divisor



So // is a C++ comment that is only available in newer versions of C.  

Declaring variables at the beginning of the block is an old C standard that you don't need in newer versions of C.  The modern standard (in C and most other languages) is to put variable declarations as close to first use as possible.  For number, you're pretty much there.  For power, it could be declared with the  initialization.  

    /* Power to generate divisor */
    int power = getNumLength(number);


I put the comment on a separate line so as to be more readable and to avoid side scroll (not so much here as in other examples).  

I would have probably have written that 

    int digit_count = count_digits(number);


That's clearer in my opinion and does not require an explanatory comment.  We're counting the number of digits and storing the result in a variable.  We'll know what the variable is later, because we call it digit_count.  

I prefer snake_case, particularly in C.  It relies less on the reader (who may not be a native English speaker) being able to recognize capital letters.  That said, camelCase is quite common.  Which to use is up to you so long as you do so consistently.  

I also changed to a four column indent.  That is more common than two column in code.  The only place that I'd recommend two column is in markup languages like HTML and XML.  They don't have methods, so their indent increases with their complexity.  With code, if you are indenting so much that you are running out of space on the screen, that may be a sign that you should push code into separate functions or methods.  

An alternate approach


  int divisor; // the divisor from power
  int i;  // loop counter

  for(i = power - 1; i &gt;=0; i--)
  {
    divisor = (int)pow(10, i);
    printf("%d  ", number / divisor);
    number = number % divisor;
  }



You are using the rather expensive pow function in each iteration of your for loop.  

Consider 

    for (int divisor = buildInitialDivisor(number); divisor &gt; 0; divisor /= BASE) {
        printf("%01d  ", number / divisor);
        number %= divisor;
    }


This declares the loop variable as part of the loop declaration.  

This uses a constant BASE instead of the magic number 10.  This improves readability and makes the program easier to modify.  

I changed from %d to %01d.  Now if you wanted to display, say, two digits at a time, you could change that to %02d and change BASE to 100.  It should zero pad the number so as to always print two digits.  Of course, it would print a single leading digit as two digits too.  That might be undesirable.  You might consider how you could fix that.  

The %= is just a shorter syntax.  Your original line and the revised one will do the exact same thing.  Assignment operators in C.  

This gets rid of pow and replaces it with a different function and then divides on each iteration.  

This gets rid of your i variable which only tracked the number of iterations.  We can do that directly.  

I prefer to always put the curly brackets on the same line as the code structure.  Either form is fine so long as you are consistent.  But you'll see both as you go, so might as well start recognizing them now.  

This also requires 

const int BASE = 10;


and 

int buildInitialDivisor(int number) {
    int divisor = 1;
    while (number / BASE &gt;= divisor) {
        divisor *= BASE;
    }

    return divisor;
}


But it gets rid of your getNumLength function.  

Recursion

You may not have gotten there yet, but when you get to recursion, you might try this problem again.  Recursion often helps when you need to reverse the natural order of output.  This is because you can display while returning from the recursion.  So rather than building the largest divisor first, you can incrementally increase the size of the divisor (or better yet, decrease the number, always using the same divisor).  

You also might try this again after learning sprintf and character arrays.