In my code, squaring the variable R always gives zero.
It does not matter if it's pow(R, 2) or R*R.
Why does that happen?
(If I change it to float, it works, but I need it to be a double for the precision.)
This is the code:
int main () {
double R,PI,a;
PI = 3.14159;
scanf(" %d", &R);
a = PI * (pow(R,2));
printf ( "A= %0.4d \n", a );
return 0;
}
You're using the wrong format specifier in scanf and printf.
The %d format specifier expects the address of an int for scanf, and an int for printf. You're instead passing in the address of a double and a double respectively. Using the wrong format specifiers invokes undefined behavior.
You need to use %lf in scanf and %f in printf for a double:
scanf("%lf", &R);
a = PI * (pow(R,2));
printf ( "A= %0.4f \n", a );
Related
I have problem with following fragment of code:
int main()
{
int n = 3;
complex_t t[3];
for(int i = 0; i < n; i++)
{
double x, y;
printf("Enter complex number: ");
scanf("%f %f", &x, &y);
t[i].re = x;
t[i].im = y;
}
}
When I am trying to pass x, and y to the program, it doesn't change value, and is 0.00000.
Can you help me?
You've declared x and y as doubles, but then went on to use the format specifier for floats (%f). So the end result is reading them in as if they were regular floats, then jamming the result into a double leaving you with unexpected values when trying to actually use them.
You need to use the format specifier specifically for doubles (%lf) here.
scanf("%lf %lf", &x, &y);
See format string specifications for more information about different format specifiers.
I have created a custom function to prompt the user with whatever you want and they need to input a double. The problem is, if I were to enter a number like 1.22 it would print '1' so it doesn't print anything behind the decimal, no rounding
int main(void) {
double f = getDouble("Enter a double: ");
printf("%d\n", f);
}
double getDouble(char* y){
double x;
printf("%s", y);
scanf("%d", &x);
return x;
}
In your code, you are using printf("%d\n", var); to print a double and you are also using scanf("%d", &x); to get a double. You should use "%lf" instead as the format specifier of in both printf in scanf. Your code obviously doesn't work because scanf is looking for an integer input and printf is trying to print and integer instead of a double.
int main(void) {
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
}
double getDouble(char* y){
double x;
printf("%s", y);
scanf("%lf", &x);
return x;
}
This should work. You can find format specifiers for every basic type: https://en.wikipedia.org/wiki/C_data_types#Main_types
Use the correct format specifier for the correct type otherwise, the function will try to do things assuming the wrong datatype which will lead to problems.
For reading double you need to use "%lf", "%d" is for integer.
double getDouble(const char* y) {
double x;
printf("%s", y);
scanf_s("%lf", &x);
return x;
}
int main()
{
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
return 0;
}
In scanf/printf use %lf instead of %d
#include <stdio.h>
double getDouble(char *y) {
double x;
printf("%s", y);
scanf("%lf", &x);
return x;
}
int main(void) {
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
}
As others have already pointed out, you are simply using the wrong specifier in scanf. They can be confusing without practice.
Here's a reference.
Write a program that uses two pointer variables to read two double numbers and display the absolute value of their sum?
this is my code and I don not know where it gets wrong:
int main(void)
{
double *p1,*p2, val1,val2;
p1 = &val1;
p2 = &val2,
printf("Enter two number: ");
scanf("%f %f", p1,p2);
if(*p1+*p2 >= 0)
printf("%f\n", *p1+*p2);
else
printf("%f\n", -(*p1+*p2));
return 0;
}
if you have to scan doubles you use "lf" and to print them as well. It was your only mistake. "f" it is just for floats.
int main(void)
{
double *p1,*p2, val1,val2;
p1 = &val1;
p2 = &val2,
printf("Enter two number: ");
scanf("%lf %lf", p1,p2);
if(*p1+*p2 >= 0)
printf("%lf\n", *p1+*p2);
else
printf("%lf\n", -(*p1+*p2));
return 0;
}
http://www.cplusplus.com/reference/cstdio/scanf/
Please consult this website or a similar one for errors or warnings.
%f is used for float values, and c compilers often issue warnings or stop compilation when type conversions occur without specifying them.
%lf is used for doubles.
I am very new to C, and while working on a project which requires pulling an indeterminate amount of values from the console, I am finding that it is not pulling the correct values. It seems like addresses, which I believe means it is a pointer issue, but I can't seem to find it.
int getVals(int degree){
double sum;
double x;
double coefs[degree];
for(int counter = 0; counter<=degree; counter = counter+1){
double nxt;
scanf(" %d", &nxt);
coefs[counter] = nxt;
printf("coefs[%d] = %d\n", counter, coefs[counter]);
}
printf(" x ? ");
scanf(" %d", &x);
printf("degree %d x %d\n", degree, x);
sum = poly(x, degree, coefs);
printf ("polynomial evaluate to: %lf\n", sum);
int newDegree;
scanf(" %d", &newDegree);
degree = newDegree;
if(degree>-1){
getVals(degree);
}
else
return degree;
}
Note: poly returns a double result of the evaluated polynomial
I am getting the following infinite loop after entering a degree of 1 and a coefficient of 1.5. It does not allow me to enter an x.
Infinite loop
In scanf(" %d", &newDegree); you should use the "%lf" format specifier (since your values is a double, not an int). Change the format specifier in all your calls to scanf() and "%f" in calls to printf().
Please refer to the documentation at this links printf(3), scanf(3).
#include <stdio.h>
#include <math.h>
double hypotenuse( double side1, double side2 );
int main( void )
{
double a, b;
printf( "Enter the values of the two sides: " );
scanf( "%f %f", &a, &b );
printf( "\nThe length of hypotenuse is: %f", hypotenuse( a, b ) );
getchar();
getchar();
return 0;
}
double hypotenuse( double side1, double side2 )
{
double c;
c = (side1 * side1) + (side2 * side2);
return sqrt( c );
}
The above program works when I use float a, b; instead of double a,b;. Why?
In your code, you use
scanf( "%f %f", &a, &b );
But %f is used for a float ,not a double. Try %lf for a double.
For scanf(), the specifier "%f" expects a float * argument, not a double *.
double a;
// v--- &a is type double *, scanf("%f" expects float *
scanf( "%f", &a );
To scan in double, use specifier "%lf" which expects a double * argument.
double a;
// v--- &a is type double *
scanf( "%lf", &a );
Detail: The arguments to scanf() are addresses of where scanf() can store the results. A float * is not the same as double *.
With printf(), numeric arguments are the value to print. Since printf() takes variable number of arguments, they go through usual promotions like float to double. Thus printf("%f", some_float) and printf("%f", some_double) can use the same format specifier. Usual promotions does not apply to &a, &b with scanf() as those argument are pointers (addresses), not arithmetic values.
Note: Always good to check scanf() results. The space between "%lf" and "%lf" is not needed as "%lf" itself consumes leading white-space, yet it is often visually easier to understand.
if (2 != scanf( "%lf%lf", &a, &b )) Handle_Error();