How do I provide a suffix for days of the month?

c, date

Solution

Here is an alternative which should work for larger numbers too:

static const char *daySuffixLookup[] = { "th","st","nd","rd","th",
                           "th","th","th","th","th" };

const char *daySuffix(int n)
{
    if(n % 100 >= 11 && n % 100 <= 13)
        return "th";

    return daySuffixLookup[n % 10];
}

Problem

I need a function to return a suffix for days when displaying text like the "`th`" in "`Wednesday June 5th, 2008`". It only need work for the numbers 1 through 31 (no error checking required) and English.

Original source

Related problems