Varying string variable in an if condition

Taking Michael Walz two great comments and adding them as an answer:

#include <stdio.h>
#include <string.h>

void main(int argc, char** argv)
{
    int mm = 0;

    printf("Please enter a month number [1-12]:\n");
    scanf("%d", &mm);
    static const char* months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

    if (mm >= 1 && mm <= 12)
    {
        printf("%d is month %s", mm, months[mm - 1]);
    }
    else
    {   
        printf("You have entered an invalid month number %d\n", mm);
    }
}

Validity check was done (mentioned in above comments).

Hope it helps.

Cheers,

Guy.

Leave a Comment