Calculate the sum of interior angles of a polygon Of the languages that I use, I have the most experience with C++. I am looking to sharpen my skills with C, so I wrote this small calculator program.

What I have here is a sample of how I would probably write larger programs.

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



int getSumInteriorAngles(const unsigned int numSides)
{
    return 180 * (numSides - 2);
}



int main(int argc, char** argv)
{
    /* Keep string constants of the questions. */

    const char* howManySides =
    "How many sides does the shape have? ";

    const char* askAnotherShape =
    "Would you like to find interior angles for another polygon? (1 or 0) ";

    /* Table of polygon names from 3 to 12 sides */
    const char* names[] = {
        "triangle",
        "quadrilateral",
        "pentagon",
        "hexagon",
        "heptagon",
        "octagon",
        "nonagon",
        "decagon",
        "hendecagon",
        "dodecagon"
    };

    /* Input variable */
    int numSides = 0;

    /* Assume that the input is invalid. */
    int sidesValid = 0;

    /* Input variable */
    int askAgain = 0;

    do {
        do {
            sidesValid = 0;

            /* Ask for the number of sides. */
            printf(howManySides);

            /* Get the user input. */
            scanf("%i", &amp;numSides);

            /* Handle invalid input. */
            if(numSides &lt; 0) {
                printf("The number of sides cannot be negative...\n");
            }
            else if(numSides == 0) {
                printf("The number of sides cannot be zero...\n");
            }
            else if(numSides &lt; 3) {
                printf("The shape must have at least 3 sides...\n");
            }
            else sidesValid = 1;
        }
        while(sidesValid == 0);

        /* If we have a name for this polygon, then get it from the table. */
        if(numSides &lt;= 12) {
            char shapeName[128];
            /* Don't forget to copy the string terminator, so add 1 to the
               copy length. */

            memcpy(shapeName, names[numSides - 3],
                strlen(names[numSides - 3]) + 1);

            printf("The sum of the interior angles of a %s is %i\n", shapeName,
                getSumInteriorAngles(numSides));
        }

        /* If we don't have a special name for this polygon, then just refer
           to it non-specifically. */

        else if(numSides &gt; 12) {
            printf("The sum of the interior angles of this polygon is %i\n",
                getSumInteriorAngles(numSides));
        }

        printf(askAnotherShape);
        scanf("%i", &amp;askAgain);

    } while(askAgain);

    return 0;
}
The most serious problem I see here is failing to check the return value from scanf().  Never ignore that - it's the only way we have to determine whether conversions were successful.

A less serious issue is that we we're using a main() that takes command-line arguments, but never use them.  Prefer instead the no-argument version:

int main(void)


The copying of the shape's name isn't useful - we can just pass the pointer directly to printf():

        printf("The sum of the interior angles of a %s is %i\n",
               names[numSides - 3],
               getSumInteriorAngles(numSides));


We could even choose which to print by using a suitable pointer:

const char* names[] = {
    "a triangle",
    "a quadrilateral",
    "a pentagon",
    "a hexagon",
    "a heptagon",
    "an octagon",
    "a nonagon",
    "a decagon",
    "a hendecagon",
    "a dodecagon"
};

// ...

const char *name = "this polygon";
if (numSides &lt;= 12) {
    name = names[numSides - 3];
}

printf("The sum of the interior angles of %s is %i\n",
       name, getSumInteriorAngles(numSides));


This also stops the code printing the ungrammatical "a octagon". :-)