To understand your error you will need an explanation of the & (or commercial) operator.
This C language operator means: memory address of ...
So 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, variables are allocated in memory and therefore have an address where they are used to access this content.
In basic language programming C, without using pointers, to access the contents of a variable simply write the name of this variable and compiler will be in charge of finding the address of it and bring or write something inside the variable.
In the case of the scanf function, it needs to know the address of the variable's memory region in order to be able to enter what is typed through the keyboard.
In your case:
printf("Digite o valor de a: \n");
scanf("%f", &a);
Above the scanf function will get the value entered by the user and put in the address of the variable a. The scanf function expects to receive this memory address from the variable and so you need to put the & in front of it and & a (this is called parameter passing by reference and you will study when viewing the subjects functions)
The printf function does not wait for you to & . The printf function only needs the name of the variable to get the content that is inside it and display this content on the screen, as long as you specify its type (% d,% f,% c, etc.).
Try, for example, to put % p instead of% f and see that the result displayed on the screen will be the value of the memory address that the variables oft, x1 and x2 occupy. p>
Note:% p indicates that you want output formatted as a memory address
Notethecodewith%p:
#include<stdio.h>#include<locale.h>#include<math.h>intmain(){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 the variable memory address now appears. It is showing in your code 0.00000 because you have formatted a float% f in the output of a memory address that should be formatted as% p.
See below for your corrected code and results. Remember that %. 0f indicates that it is not to have decimals after the comma of a real number (float)
If it were %. 2f it would indicate that you want two decimal places after the comma. and so it goes ...
#include<stdio.h>#include<locale.h>#include<math.h>intmain(){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 C concepts because it will help you a lot! Write down your questions and do not hesitate to ask! Hugs!
Remember: H err U hand! :)