Program to display a matrix in a spiral form I found an interview question which requires us to print the elements of the matrix in a spiral order starting from the top left. I need some pointers on how to improve my code:

#include &lt;stdio.h&gt;

//function that prints a row from startx,starty to endy
void printRow(int arr[][4],int startx,int starty,int endy)
{
    int yCtr;
    for(yCtr = starty; yCtr &lt;= endy ; yCtr ++)
       printf("%d ",arr[startx][yCtr]);
}

//function that prints a row from startx,starty to endy (decreasing columns)
void printRowBackward(int arr[][4],int startx,int starty,int endy)
{
    int yCtr;
    for(yCtr = starty; yCtr &gt;= endy ; yCtr --)
       printf("%d ",arr[startx][yCtr]);
}

//function that prints a column from startx,starty to endx
void printColumnBackward(int arr[][4],int startx,int starty,int endx)
{
   int xCtr;
    for(xCtr = startx; xCtr &gt;= endx; xCtr --)
       printf("%d ",arr[xCtr][starty]);
}

// column backwards
void printColumn(int arr[][4],int startx,int starty,int endx)
{
    int xCtr;
    for(xCtr = startx; xCtr &lt;= endx; xCtr ++)
       printf(" %d ",arr[xCtr][starty]);
}

// prints a section of the spiral
void printSpiralSection(int arr[][4],int startx,int starty,int size)
{
   printRow(arr,startx,starty,size - 1);
   printColumn(arr,startx + 1 ,size - 1 ,size -1);
   printRowBackward(arr,size - 1,size - 2,starty);
   printColumnBackward(arr,size - 2,starty,startx + 1);
}

int main()
{
    int array[4][4] = { 22 ,323,2342,222,
                          2,234,243,333,
                          21,13,23,444,
                          223,234,231,234};

    int startx =0, starty = 0, size = 4;
    // prints each section of the spiral ... 
    while(size &gt;=1) {
       printSpiralSection(array,startx,starty,size);
       size --;
       startx++;
       starty++;
    }
    getchar();
    return 0;
}
I would test it with a bigger array.  Since I created one to test it, I might as well post it here.  I put the numbers in the order I expected them to be printed, which makes checking easier.

const int array[][ARR_SIZE] = {
    { 1,   2,  3,  4,  5,  6,  7,  8,  9 },
    { 32, 33, 34, 35, 36, 37, 38, 39, 10 },
    { 31, 56, 57, 58, 59, 60, 61, 40, 11 },
    { 30, 55, 72, 73, 74, 75, 62, 41, 12 },
    { 29, 54, 71, 80, 81, 76, 63, 42, 13 },
    { 28, 53, 70, 79, 78, 77, 64, 43, 14 },
    { 27, 52, 69, 68, 67, 66, 65, 44, 15 },
    { 26, 51, 50, 49, 48, 47, 46, 45, 16 },
    { 25, 24, 23, 22, 21, 20, 19, 18, 17 },
};


Define ARR_SIZE at the top to 9 and replace each explicit 4 (apart from those in the array of course) with ARR_SIZE. Make each function static and each array parameter const.

Having done this I found that it really does work :-)



Each of your for-loops would be better written:

for (int y = starty; y &lt;= endy; y ++) {
   printf("%d ", arr[startx][y]);
}


Notice that I defind the loop variable within the loop and called it y
instead of yCtr (the Ctr was just noise) and added some spaces. Note that it is normal to put a space after a comma (applies everywhere in your code - your use of spaces is occasionally inconsistent).  I prefer start_x to startx and print_row to printRow,  but that it just personal preference. 

Also, a minor point, as the functions are named printRow and printColumn etc, it might be more natural to express the parameters in terms of rows and columns instead of x and y:

static void printRow(const int arr[][ARR_SIZE], int row, int col, int end_col)
{
    for (; col &lt;= end_col ; ++col) {
       printf("%d ", arr[row][col]);
    }
}




The main loop would normally be expressed as a for-loop

int startx = 0;
int starty = 0;
for (int size = ARR_SIZE; size &gt;= 1; --size) {...


Note that I defined the two start variables on separate line - again this is considered best practice.

Final point, your comments are mostly noise.