Skip to main content
2 of 9
edited title
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65

Printing a multiplication table in C

I'm a complete beginner, but I'd like to learn how to program.

So I tried the common exercice, aka the multiplication table, but I tried to add little things I learnt recently in order to challenge myself, like functions, tables and pointers even if it's useless here.

The code works well, but I have some warnings when compiling, and I don't know why, like *pointSize = NULL;

Besides, I'm sure my code could be cleaned up, so I'd like you to tell me how I can improve it.

Thanks a lot.

Here's the result: enter image description here

/* Multiplication table */

#include <stdio.h>
unsigned short tableSize; // Number of multipliers
unsigned short question (unsigned short *pointSize); // Ask user how many multipliers
void multTable(); // The table

int main ()
{
    unsigned short i = 1, nbMultipliers;
    question(&nbMultipliers);
    tableSize = nbMultipliers;
    
    /* header */
    printf("       |");
    for (i = 1; i <= tableSize; i++)
    {
        printf("  %3u  |", i);
    }
    printf("\n");

    for (i = 1; i <= (tableSize + 1); i++)
    {
        printf ("========");
    }
    printf("\n");
   /* end header */

    multTable(); // the table
}

/* Ask user how many multipliers */
unsigned short question (unsigned short *pointSize)
{
    *pointSize = NULL;

    printf("How long this table should be ? ");
    scanf("%u", pointSize);
    printf("\n");
}
/* end Ask user how many multipliers */

void multTable()
{
    unsigned short i = 1, j = 1;
    unsigned short line[10] = {0};
 
    for (j = 1; j <= tableSize; j++)
    {
        printf("  %3u  I", j);
        for (i = 1; i <= tableSize; i++)
        {
            line[i] = i*j;
            printf ("  %3u  |", line[i]);
        }
        printf("\n");
        for (i = 1; i <= (tableSize + 1); i++)
        {
            printf ("-------+");
        }
        printf("\n");
    }

}