Ashish Rawat
Ashish Rawat

Reputation: 3513

Value of -1 in return statement

I want to know that how it print the line when value of return is -1. Also, I don't know about what does the -1 do, like 1 for true and 0 for false, but what is for -1.

#include <stdio.h>

struct date {
    int day, month, year;
};

int compare_dates(struct date d1, struct date d2) {
    if(d1.year < d2.year)
        return -1;
    else if(d1.year > d2.year)
        return 1;
    else if(d1.month < d2.month) 
            return -1;
    else if(d1.month > d2.month) 
            return 1;
    else if(d1.day < d2.day) 
            return -1;
    else if(d1.day > d2.day)
            return 1;
    else
        return 0;


}

int main(void) {
    struct date d1, d2;
    printf("Enter first date (dd/mm/yyyy): ");
    scanf("%2d/%2d/%4d", &d1.day, &d1.month, &d1.year);
    printf("Enter second date (dd/mm/yyyy): ");
    scanf("%2d/%2d/%4d", &d2.day, &d2.month, &d2.year);

    if(compare_dates(d1, d2)) 
        printf("Date1 comes after than Date2");
    else if(!compare_dates(d1, d2))
        printf("Date1 and Date2 are equal");
    else if(-1)
        printf("Date1 comes before than Date2");

}

Upvotes: 0

Views: 121

Answers (6)

Keith Thompson
Keith Thompson

Reputation: 263217

If you had a function called dates_are_equal, returning 0 or 1 (or more generally 0 or non-0), it would make sense to use the result directly as a condition:

if (dates_are_equal(d1, d2)) {
    printf("Date1 and Date2 are equal\n");
}
else {
    printf("Date1 and Date2 are not equal\n");
}

But your compare_dates function can return any of three different integer values, with three distinct meanings. The result is a number, not a condition, so don't treat it as a condition. Instead, build a condition by comparing the result to another integer.

There are several ways you can do this. By analogy with strcmp(), which indicates the result of the comparison by returning a value less than, equal to, or greater than 0 (not necessarily just -1, 0, or -1), you can write:

int comparison = compare_dates(d1, d2);
if (comparison < 0) {
    printf("Date1 comes before Date2\n");
}
else if(comparison == 0) {
    printf("Date1 and Date2 are equal\n");
}
else { // No need to test again here
    printf("Date1 comes after Date2\n");
}

If you want to assume that compare_dates() can only return -1, 0, or 1, you can use a switch statement:

switch (compare_dates(d1, d2)) {
    case -1:
        printf("Date1 comes before Date2\n");
        break;
    case 0:
        printf("Date1 and Date2 are equal\n");
        break;
    case 1:
        printf("Date1 comes after Date2\n");
        break;
    default:
        fprintf(stderr, "Internal error, unexpected result\n");
        exit(EXIT_FAILURE);
}

I don't recommend using (!comparison) in place of (comparison == 0). It's perfectly legal, and it means exactly the same thing to the compiler, but in my opinion it's better to reserve the ! operator for values that really are logically boolean.

It's common practice to use treat non-boolean expressions as conditions, such as writing

if (!strcmp(s1, s2)) {
    /* the strings pointed to by s1 and s2 are equal */
}

or

if (!ptr) {
    /* ptr is a null pointer */
}

but I find that being more explicit makes the code easier to read:

if (strcmp(s1, s2) == 0) {
    /* the strings pointed to by s1 and s2 are equal */
}

or

if (ptr != NULL) {
    /* ptr is a null pointer */
}

Upvotes: 1

autistic
autistic

Reputation: 15632

Actually, 0 signifies that d1 and d2 are equal. Allow me to explain:

-1 means d1 is less than d2. Hence the "less than" operator < corresponding to -1. 0 means d1 is equal to d2. Hence the "equal to" operator == corresponding. 1 means d1 is greater than d2. Hence the "greater than" operator > corresponding to 1.

This is just convention, probably influenced by the C standard strcmp function which uses the same convention.

Upvotes: 0

flyingOwl
flyingOwl

Reputation: 244

In C everything that is != 0 is true.

if(-1) // = true
if(0)  // = false
if(1)  // = true
if(1234567) // = true

To print a message depending on the return value, use switch:

switch(compare_dates(d1, d2)){
    case -1: {
        // do whatever should done when compare_dates returns -1
        break; //don't forget the break
    }
    case 0: {
        // do whatever should done when it returns 0
        break;
    }
    case 1: {
        // ...
        break;
    }
}

Upvotes: 0

Cristiano Sousa
Cristiano Sousa

Reputation: 932

In C, 0 is False, everything else is True.

In your code you would have to do

if(compare_dates(d1, d2) == 1) 
    printf("Date1 comes after than Date2");
else if(!compare_dates(d1, d2))
    printf("Date1 and Date2 are equal");
else if(compare_dates(d1, d2) == -1)
    printf("Date1 comes before than Date2");

or, more efficient

int value = compare_dates(d1, d2)
if(value == 1) 
    printf("Date1 comes after than Date2");
else if(!value)
    printf("Date1 and Date2 are equal");
else if(value == -1)
    printf("Date1 comes before than Date2");

For compare functions, 0 usualy means the values are equal, -1 means the first argument is smaller than the second, and 1 means that the first argument is larger than the second

Upvotes: 1

Jay
Jay

Reputation: 24895

By looking at your compare_dates function it appears that 1 will be returned if date d1 is greater than date d2, -1 will be returned if date d1 will be lesser than date d2 and 0 will be returned if both dates are same.

So, your code should be like below:

   int main(void) 
   {
        struct date d1, d2;
        int result;
        printf("Enter first date (dd/mm/yyyy): ");
        scanf("%2d/%2d/%4d", &d1.day, &d1.month, &d1.year);
        printf("Enter second date (dd/mm/yyyy): ");
        scanf("%2d/%2d/%4d", &d2.day, &d2.month, &d2.year);

        result = compare_dates(d1, d2);
        if(1 == result)
            printf("Date1 comes after than Date2");
        else if(0 == result)
            printf("Date1 and Date2 are equal");
        else if(-1 == result)
            printf("Date1 comes before than Date2");

   }

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249123

This:

if(compare_dates(d1, d2)) 
    printf("Date1 comes after than Date2");
else if(!compare_dates(d1, d2))
    printf("Date1 and Date2 are equal");
else if(-1)
    printf("Date1 comes before than Date2");

Is messed up. You should call compare_dates a single time and compare its result against the various possibilities.

In particular, if(-1) is nonsense, since it will always be true.

Upvotes: 1

Related Questions