未定义的行为计数天数(练习7-2)[实用C编程]

未定义的行为计数天数(练习7-2)[实用C编程],c,C,我正在阅读实用的C编程书,似乎不知道我把事情搞砸了。我的程序计算到日期之间的天数的代码是: /* Write a program to perform date arithmetic such as how many days there are * between 6/6/90 and 4/3/92. Include a specification and a code design. */ /************************************************

我正在阅读实用的C编程书,似乎不知道我把事情搞砸了。我的程序计算到日期之间的天数的代码是:

/* Write a program to perform date arithmetic such as how many days there are
 * between 6/6/90 and 4/3/92. Include a specification and a code design. */

/****************************************************************
 * This program will ask a user to enter in a beginning date    *
 * and an ending date formated as MM/DD/YYYY and calculate the  *
 * number of days between those two dates.                      *
 ***************************************************************/

#include <stdio.h>

//function to check leap years.
int leapYear(int year) {

    if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0)
        return 1;
    else
        return 0;

}

//function to calculate the days between to dates.
int daysBetweenDates(int start_date[], int end_date[]) {
    int days = 0; /*****THIS IS WHERE THE ERROR WAS I DIDN'T INITIALIZE THE VARIABLE*****/ //variable to keep track of the days counted.
    int month_counter, year_counter; //counters for the current month and year in the loops.

    //loop over the years
    for (year_counter = start_date[2]; year_counter <= end_date[2];
            year_counter += 1) {

        if (year_counter == start_date[2]) //if we're in the beginning year,
            month_counter = start_date[0]; //start the month counter at the given start month.
        else
            month_counter = 1; //otherwise start in January.

        //loop through the months
        while (month_counter <= 12) {

            //if we're in the final month of the final year we break and add the days in that month separately.
            if (year_counter == end_date[2] && month_counter == end_date[0])
                break;

            //add the number of days in the current month to the days variable.
            switch (month_counter) {

            case 1:
                days += 31;
                break;
            case 2:
                days += 28;
                //add extra day for leap years.
                if (leapYear(year_counter))
                    days += 1;
                break;
            case 3:
                days += 31;
                break;
            case 4:
                days += 30;
                break;
            case 5:
                days += 31;
                break;
            case 6:
                days += 30;
                break;
            case 7:
                days += 31;
                break;
            case 8:
                days += 31;
                break;
            case 9:
                days += 30;
                break;
            case 10:
                days += 31;
                break;
            case 11:
                days += 30;
                break;
            case 12:
                days += 31;
                break;
            default:
                break;

            }

            //next month.
            month_counter += 1;
        }

    }

    //add difference of the days from the start and end dates. (why we skipped the last month.)
    days += (end_date[1] - start_date[1]);

    return days;
}

//function to make sure dates given are valid.
int checkDates(int start_date[], int end_date[]) {
    if (start_date[2] > end_date[2])
        return 1;

    if (start_date[2] == end_date[2] && start_date[0] > end_date[0])
        return 1;

    if (start_date[2] == end_date[2] && start_date[0] == end_date[0]
            && start_date[1] > end_date[1])
        return 1;

    else
        return 0;

}

int main() {
    int start_date[3];
    int end_date[3];
    char last_day[3];
    int add_last_day;


    printf("Please enter in the start date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &start_date[0], &start_date[1], &start_date[2]);

    printf("Please enter in the end date (MM/DD/YYYY): ");
    scanf("%d/%d/%d", &end_date[0], &end_date[1], &end_date[2]);

    if(checkDates(start_date, end_date)){
        printf("Invalid Dates.\n");
        return 1;
    }

    printf("Should the last day be counted? (Y/N) ");
    scanf("%s", last_day);

    if(last_day[0] == 'Y' || last_day[0] == 'y')
        add_last_day = 1;
    else
        add_last_day = 0;


    printf("between %d/%d/%d and %d/%d/%d there are %d days.\n", start_date[0],
            start_date[1], start_date[2], end_date[0], end_date[1], end_date[2],
            daysBetweenDates(start_date, end_date) + add_last_day);

    return 0;
}

这显然是错误的。我缺少什么?

尝试使用int days=0;我过去犯过这样一个愚蠢的错误:P谢谢!我还在学习C,我使用-Wall-Wextra编译,它没有捕获初始化错误。每次我写程序的时候都要记住这一点。但是你的程序显示的天数是错误的。。我知道这不仅仅是一个好的练习,这是必须的,否则你会有一种不确定的行为,就像我刚才所做的那样,我只是还没有让它烧到我的头上:P我很快就会得到它@ANBU.SANKAR怎么会这样?我用一个基于网络的日计算器检查了它,它显示了相同的值,不算最后一天,如果这是你的意思?
$ ./ex_7-2
Please enter in the start date (MM/DD/YYYY): 1/1/2014
Please enter in the end date (MM/DD/YYYY): 1/31/2014
between 1/1/2014 and 1/31/2014 there are 32797 days.