To understand your error you will need an explanation about the operator & (í commercial) or (and commercial).
This operator in C language means: memory address of...
Therefore, if you put this operator in front of some variable it will return the address that the variable is allocated in RAM.
As you know, the variables are allocated in memory and therefore have an address where it is used to access this content.
In basic programming of C language, without use of pointers, to access the contents of a variable just write the name of this variable and compiler takes care of finding its address and bring or write something inside the variable.
In the case of the function scanf she needs to know the address of the memory region of the variable to be able to put what is typed via keyboard, inside the variable.
In your case:
printf("Digite o valor de a: \n");
scanf("%f", &a);
Above the function scanf will get the value typed by the user and put in the address of the variable a. The scanf function expects to receive this memory address of the variable and so you need to put the & in front of getting her &to (this is called passing parameters by reference and you will study when you see the functions subjects)
Already the function printf waiting that you put this &. The printf function only needs the variable name to get the content inside it and display this content on the screen, provided you specify its type (%d, %f , %c, etc).
Try, for example, putting %p instead of %f and see that the result shown on the screen will be the value of the memory address that the variables Delt, X1 and x2 occupy.
Note: %p indicates that you want the output formatted as a memory address
Observe the code with %p:
#include <stdio.h>
#include <locale.h>
#include <math.h>
int main() {
setlocale(LC_ALL, "Portuguese");
float a, b, c, delt, x1, x2;
printf("Digite o valor de a: \n");
scanf("%f", &a);
printf("Digite o valor de b: \n");
scanf("%f", &b);
printf("Digite o valor de c: \n");
scanf("%f", &c);
delt = b*b -4*a*c;
printf("Delta = %p \n", &delt);
if (delt >= 0) {
x1 = (-b + sqrt(delt))/(2*a);
x2 = (-b - sqrt(delt))/(2*a);
printf("x1 = %p \n", &x1);
printf("x2 = %p \n", &x2);
}
else {
printf("A equação não tem raizes. \n");
}
return 0;
} /* fim main*/
Note that now appears the memory address of the variables. It is appearing in your code 0.00000 because you have formatted a %f float in the output of a memory address that should be formatted as %p.
See now below your corrected code and the results. Remembering that %.0f indicates that we do not have decimals after the comma of a real number(float)
If it were %.2f would indicate that you want two decimal places after the comma. and so on...
#include <stdio.h>
#include <locale.h>
#include <math.h>
int main() {
setlocale(LC_ALL, "Portuguese");
float a, b, c, delt, x1, x2;
printf("Digite o valor de a: \n");
scanf("%f", &a);
printf("Digite o valor de b: \n");
scanf("%f", &b);
printf("Digite o valor de c: \n");
scanf("%f", &c);
delt = b*b -4*a*c;
printf("Delta = %.0f \n", delt);
if (delt >= 0) {
x1 = (-b + sqrt(delt))/(2*a);
x2 = (-b - sqrt(delt))/(2*a);
printf("x1 = %.0f \n", x1);
printf("x2 = %.0f \n", x2);
}
else {
printf("A equação não tem raizes. \n");
}
return 0;
}
Ready! Now it works! Study the concepts of the C language as it will help you a lot! Write down your questions and do not hesitate to ask! Hug!
Remember: Herr is Ubro! :)