Error: incompatible type for argument 1 of 'printf'

Asked

Viewed 1,022 times

3

I have this activity, exercise, where is to display the largest number, but is giving the error:

"incompatible type for argument 1 of 'printf' "

On the command line "printf (valor2);".

Code:

#include <stdio.h>
main()

{
   double valor1, valor2;
   scanf ( "%lf", &valor1);
   scanf ( "%lf", &valor2);
    if ( valor1>valor2)
   {
      print ( valor1);
    }
      else if (valor1< valor2)
     {
        printf (valor2)
      }
}
  • Did the answer solve your question? Do you think you can accept it? See tour if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

2 answers

4

There are several errors in your code, I believe because you are a beginner! I recommend reading about printf and scanf functions

Read this answer too Difference between %i and %d

Your functional code looks like this:

#include <stdio.h>
int main()

{
   double valor1, valor2;
   scanf ( "%lf", &valor1);
   scanf ( "%lf", &valor2);
    if ( valor1>valor2){             
      printf("%f", valor1);
            }
      else if (valor1 < valor2){
            printf("%f", valor2);
      }
}

Just a small hint, as you will compare only 2 variables, could do with the structure if-else instead of if-elseif:

#include <stdio.h>
int main()

{
   double valor1, valor2;
   scanf ( "%lf", &valor1);
   scanf ( "%lf", &valor2);
    if ( valor1>valor2){             
      printf("%f", valor1);
            }
      else {
            printf("%f", valor2);
      }
}

As recalled by the Cat, there is still a third possibility using ternary operator, or conditional operator:

Read on this question: When should I use the "?" operator in C?

#include <stdio.h>
int main()

{
   double valor1, valor2, resultado;
   scanf ( "%lf", &valor1);
   scanf ( "%lf", &valor2);
   resultado = valor1 > valor2 ? valor1 : valor2;
   printf("%f", resultado);

}
  • In this case, the use of the ternary operator could also be useful to improve the readability of the code.

  • @cat well remembered! Do you think it is advisable to post your question to him? https://answall.com/questions/92101/quando-devo-usar-o-operador-em-c

  • 1

    You can quote in your reply.

2

I believe it’s a syntax error in printf, try to write this way:

printf("%d", valor2)

Note also that you used print(valor1) and changed to print"f" (value2).

Browser other questions tagged

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