-6

This program is supposed to input a number and its square value, then tell me if right or wrong. I have some problems but I can't figure out what they are.

#include <stdio.h>
#include <math.h>
int main()
{
    float P;
    float q;
    float r;
    printf("Enter the value of p\n");
    scanf("%f",p);
    q= p*p;
    printf("Enter the square value of %f \n",p);
    scanf("%f",r);
    if (r = q){
        printf("You are right\n");
    }
    else{
        printf("you are wrong\n");
    }
    return 0;
}

so tell me my mistakes

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
user101134
  • 111
  • 3

1 Answers1

2

Please compile the program with flags -Werror -Wall -Wextra although the first mistake is always a compilation error (typo): replace float P; with float p; because C is case-sensitive.

Then you need to pass the address of a variable to scanf, these two lines

scanf("%f",r);
...
scanf("%f",p);

should be

scanf("%f",&r);
...
scanf("%f",&p);

Lastly, there is a syntax error where you test for equality with

if (r = q)

but this changes r and tests if it is non-0. With an integer type you should use

if (r == q)

but with floating point types, equality tests don't work well, please see why in this question.

Community
  • 1
  • 1
jdarthenay
  • 3,062
  • 1
  • 15
  • 20