What is wrong in the double variable

Asked

Viewed 79 times

1

I need to make an algorithm that the user informs two values and return the biggest among them using double, but when compiling it from the error pointing something in double but do not know how to solve, someone can help me and explain how I would use correctly?

#include <stdio.h>
double maior(double *x, double *y)    
{  
    double maior;   

    if (*x>*y)
    {
        maior=*x;
        return maior;
    }

    else
    {
        maior=*y;
    }

    return maior;
}



int main()  
{   

    double x,y,m;

    printf("Informe dois valores com espaco entre eles:");
    scanf("%f %f",&x,&y);

    m=maior(&x,&y);

    printf("O maior eh: %f \n",m);


    return 0;
}
  • 1

    And the last printf the printf("O maior eh: \n",m); missing the formatter for the m

1 answer

6


The problem that is happening is due to the fact that you are trying to return a variable of type double in a role that supposedly should return a int. To return the double, change the function return type to double:

double maior(double *x, double *y) {  
    double m;   

    if (*x>*y)
    {
        m=*x;
        return m;
    }

    else
    {
        m=*y;
    }

    return m;
}

printf("O maior é: %f\n", m); // Faltou o formatador

Just adding, its function could be reduced to the following code:

double maior(double x, double y) {
    if(x < y)
        return y;
    return x;
}
  • 2

    return x < y ? y : x;.

  • I did that, but still misspelling the compilation by saying "Warning: format ?%f' expects argument of type ' float *', but argument 2 has type ?double *' [-Wformat=] scanf("%f %f",&x,&y);" which may be?

  • @Flavio Use maior(x, y) instead of maior(&x, &y).

  • @Victorstafusa I am obliged to pass by reference the value of x e y in the program, the problem e q n ta compiling it

  • 4

    @lavium scanf("%lf %lf", &x, &y); - The %lf is for double while the %f is for float.

  • worked now @Victorstafusa, needed the lf, vlw bro

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.