c语言某年某月有多少天

刚开始学,这个程序无论我输入几年几月输出的都是error,可以麻烦帮我看下为什么一直输出是error吗

img

是因为scanf("&d,%d", &year,&month)里面的&d导致从输入获取年月失败,然后程序根据变量year, month变量内存位置原来的值进行的判断每个月有多少天,所以导致程序出错,可以在scanf()函数前后用printf打印month和year的值来进行观察。

把scanf()函数那里改为:scanf("%d,%d", &year,&month), 然后你输入的格式按照scanf函数里面的格式输入,例如:2019,3.修改如下:

#include  <stdio.h>

int main(void){
    
    int year,month;
//    printf("没有获取输入前,year=%d,month=%d\n",year,month);
    printf("输入年,月:");
    scanf("%d,%d",&year,&month);
//    printf("year=%d,month=%d\n",year,month); 
    switch(month){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:printf("31 days");
                break;
        case 2: if((year%4==0&&year%100!=0)||(year%400==0)){
                    printf("29 days");        
                }else{
                    printf("28 days");
                }
                break;
        case 4:                
        case 6:                
        case 9:                
        case 11:printf("30 days");
                break;
        default:
                printf("error!");                
    } 
    
    return 0;
} 

img

scanf("%d,%d"
第一个你写成&d了啊