Alexandros Voliotis
Alexandros Voliotis

Reputation: 318

error: conflicting types, error: type of formal parameter 2 is incomplete

I'm writing a code that calculates the difference in days between 2 given dates. Note that each month is considered to be equal to 30 days and each year equal to 360 days.

I'm getting the following warning/errors and I don't seem to understand why:

Warning&Errors

Here's my code:

#include <stdio.h>

int dif_days(struct Date Date1, struct Date Date2);

struct Date
{
    int Day;
    int Month;
    int Year;
};

int main()
{
    struct Date Date1;
    struct Date Date2;

    printf("\n Please enter the first date: ");
    scanf("%d %d %d ", &Date1.Day, &Date1.Month, &Date1.Year);

    printf("\n Please enter the second date: ");
    scanf("%d %d %d ", &Date2.Day, &Date2.Month, &Date2.Year);

    int diff = dif_days(Date1, Date2);
    printf("\n The difference in days is: %d \n", diff);

    return 0;
}

int dif_days(struct Date Date1, struct Date Date2)
{
    // variable declaration 
    int difference;
    int Day, Month, Year; // The final days/months/years

    // for the days
    if (Date1.Day > Date2.Day)
        Day = Date1.Day - Date2.Day;
    else    
        Day = Date2.Day - Date1.Day;

    // for the months
    if (Date1.Month > Date2.Month) 
        Month = Date1.Month - Date2.Month;
    else
        Month = Date2.Month - Date1.Month;

    // for the years
    if (Date1.Year > Date2.Year)
        Year = Date1.Year - Date2.Year;
    else        
        Year = Date2.Year - Date1.Year;       

    difference = Day + Month*30 + Year*360;                

    return difference;
}

Upvotes: 1

Views: 778

Answers (1)

ad absurdum
ad absurdum

Reputation: 21365

You need to declare struct Date before you use it in the function prototype for dif_days().

You can either move the entire definition of the struct so that it comes before the function prototype, or you can use a forward declaration for the struct by adding before the function prototype:

struct Date;

Also, you need to remove the trailing whitespace characters from your scanf() format strings. This plays havoc with interactive input, and does not do what people usually seem to expect. Note that the %d directive automatically ignores leading whitespace characters, and in fact the only scanf() directives that do not ignore leading whitespaces are %c, %n, and %[].

And while we're on the topic of scanf(), you should be checking the value returned by calls to scanf() to ensure that input is as expected. Once a match fails in the format string, scanf() will move on without storing anything in the remaining variables. Invalid inputs will lead to undefined behavior when the code attempts to use indeterminate values. Here scanf() will return 3 if three numbers are entered, and for basic validation you could check that the user did in fact enter three numbers before continuing.

Upvotes: 2

Related Questions