-5

I was trying to make a program on gregorian calendar but when I tried assigning month names to string in if else block as well as switch case , codeblocks stopped responding at runtime.

 int main()
{
    int month,year;
    scanf("%d %d",&month,&year);
    char month1[10];
    if(month==1)
        month1[10]="January";
    else if(month==2)
        month1[10]="February";
    else if(month==3)
        month1[10]="March";
    else if(month==4)
        month1[10]="April";
    else if(month==5)
        month1[10]="May";
    else if(month==6)
        month1[10]="June";
    else if(month==7)
        month1[10]="July";
    else if(month==8)
        month1[10]="August";
    else if(month==9)
        month1[10]="September";
    else if(month==10)
        month1[10]="October";
    else if(month==11)
        month1[10]="November";
    else if(month==12)
        month1[10]="December";
    printf("%s %d",month1,year);
    return 0;
}

similarly when i used to assign string values in switch case, then also runtime error occurred

main()
{
    int month,year;
    scanf("%d %d",&month,&year);
    char month1[15];
    switch(month)
    {
    case 1:
        month1[15]="January";
        break;
    case 2:
        month1[15]="February";
        break;
    case 3:
        month1[15]="March";
        break;
    case 4:
        month1[15]="April";
        break;
    case 5:
        month1[15]="May";
        break;
    case 6:
        month1[15]="June";
        break;
    case 7:
        month1[15]="July";
        break;
    case 8:
        month1[15]="August";
        break;
    case 9:
        month1[15]="September";
        break;
    case 10:
        month1[15]="October";
        break;
    case 11:
        month1[15]="November";
        break;
    case 12:
        month1[15]="December";
        break;
    default:
        printf("wrong input");
    }
    printf("%s %d",month1,year);
    //char calender[6][7];
}

why it is happening?

PiyushM
  • 23
  • 6
  • First of all, you overflow your array; the maximum index for the month array is 9 or 14 depending on your version of your code. Also, you cannot assign strings that way. You declare a character array, not an array of character arrays. – clarasoft-it Jul 28 '16 at 15:51

2 Answers2

5

You do not assign C strings like that, because strings are implemented as null-terminated sequences of char values. If all possible values are string literals, you could use a char * pointer instead, like this:

char *month1;
if(month==1)
    month1="January";
else if(month==2)
    month1="February";
...

However, an even better approach is to build a look-up table, and get month name using array subscript operator:

char *months[] = {"January", "February", "March", ...}
if (month >=1 && month <= 12) {
    printf("%s %d", months[month-1], year);
} else {
    printf("wrong input");
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

In C, strcpy() is the correct way to assign values to strings.

Try: strcpy(month1, "YourMonth")

SegFault
  • 2,526
  • 4
  • 21
  • 41