Warning: format '%f' expectts a matching 'double' argument (en)(en)

Asked

Viewed 98 times

2

My first post here and I would like to know, because when I compile my code it gives an error saying the format %f expecting a double, whereas the f is used to float and the variable I’m putting is float.

Follows the code:

#include <stdio.h>

float proceNotas(float nota1, float nota2, float nota3, char letter);

int main(){
    float n1, n2, n3;
    char letra;
    printf("Digite a primeira nota: ");
    scanf("%f", &n1);
    printf("Digite a segunda nota: ");
    scanf("%f", &n2);
    printf("Digite a terceira nota: ");
    scanf("%f", &n3);
    getchar();
    printf("Digite a operacao desejada: A) media aritmetica\nP) Media ponderada\nH) Media harmonica\n");
    letra = getchar();
    printf("\nA media do aluno é: %f"), proceNotas(n1, n2, n3, letra);
    return 0;
}

float proceNotas(float nota1, float nota2, float nota3, char letter){
    float arit, pond, harm;

    switch(letter){
        case 'A':
        arit = nota1+nota2+nota3/3;
        return arit;
        break;
        case 'P':
        pond = (nota1*5)+(nota2*3)+(nota3*2)/5+3+2;
        return pond;
        break;
        case 'H':
        harm = 3/(1/nota1)+(1/nota2)+(1/nota3);
        return harm;
        break;
        default:
        return printf("Operacao invalida!");
    }
}

Segue o print com o erro no console

2 answers

5


Several errors.

Here:

printf("\nA media do aluno é: %f"), proceNotas(n1, n2, n3, letra);

should be:

printf("\nA media do aluno é: %f", proceNotas(n1, n2, n3, letra));

Your calculations are wrong. Study operator precedence.

Here:

arit = nota1+nota2+nota3/3;

I believe that you do not want to divide nota3 by 3 but the whole sum:

arit = (nota1+nota2+nota3)/3;

Here:

pond = (nota1*5)+(nota2*3)+(nota3*2)/5+3+2;

must be:

pond = ((nota1*5)+(nota2*3)+(nota3*2)) / (5+3+2);

and here:

harm = 3/(1/nota1)+(1/nota2)+(1/nota3);

must be:

harm = 3 / ((1/nota1)+(1/nota2)+(1/nota3));
  • I feel even ashamed, thank you very much, looking like this, are several silly mistakes

  • @Axelfelix, accept the reply of anonymity :). He replied but you did not mark his reply as useful and did not accept the reply

  • can you answer my question? https://answall.com/questions/469237/identificar-registrations-sequentialof each user

0

The function printf() is a function with variable argument number (with ... in the prototype). Thus, the arguments fall under the domain of ... suffer the preset promotions for arguments (link in English)

... The default argument promotions are performed on trailing Arguments.

These preset promotions include converting type values float for standard values double.

That is, it is not possible to send a type value float into the printf(). It makes no sense to have a conversion to this type, so the conversion "%f" is for type double.

    float xf; double xd; long double xl;
    xf = xd = xl = 3.14159265L;
    printf("%f %lf\n", xf, xf);       // %f ou %lf para float/double
    printf("%f %lf\n", xd, xd);
    printf("%Lf\n", xl);              // %Lf para long double

https://ideone.com/nr6nqy

Browser other questions tagged

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