getDouble function in C - c

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.

Related

Complex numbers through scanf in C

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.

Squared Equation calculator not working in C

Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.

My code produces -nan on changing data type from float to double

int main(void) {
float a;
scanf("%f", &a);
double c = sqrt(a);
printf("%f", c);
return 0;
}
A float variable results in correct output.
However, on changing the data type to double result is NaN.
You should use the format specifier %lf for taking in a double.
#include<stdio.h>
#include<math.h>
int main(void) {
double a;
scanf("%lf", &a);
double c = sqrt(a);
printf("%lf", c); //printf("%f", c) also works perfectly.
return 0;
}
Since you are possibly using just %f, you get NaN.

Double Exponent from user input

So my question relates to double, I am trying to get an input from the user in decimal point for any value and its exponent also in decimal point to display the result after calculation in another function where the variables will pass values as double and I have used double the output as well but the end result is 1.00000 even though I have used the output specifier as %lf%.
#include <stdio.h>
double pwra (double, double);
int main()
{
double number, power, xx;
printf("Enter Number: ");
scanf("%lf", &number);
printf("Enter Number: ");
scanf("%lf", &power);
xx=pwra (number,power);
printf("Result: %lf", xx);
return 0;
}
double pwra (double num, double pwr)
{
int count;
int result = 1;
for(count=1;count<=pwr;count++)
{
result = result*num;
}
return result;
}
You have used the wrong type for result in the pwrs() function.
Change:
int result = 1;
to:
double result = 1.0;
Note that this type of simple mistake is easily identified if you learn to use your debugger. Further reading: How to debug small programs.
Also note that pwr should be an int, not a double, since your function only works with integer exponents.

printf in Eclipse

I have to work with eclipse in C. I wrote a simple program, but I have a problem with a printf command which doesn't work properly. Any idea?
Here is the code :
#include <stdio.h>
void change(double *x, double *y)
{
double help = *x;
*x = *y;
*y = help;
return;
}
int main()
{
double x=0, y=0;
printf("please give a value to a \n ");
scanf("%f",&x);
printf("please give a value to b \n");
scanf("%f",&y);
printf("x=%.2f\t y=%.2f\n",x,y);
printf("will give \n");
change(&x,&y);
printf("x=%.2f\t y=%.2f\n",x,y);
return 0;
}
So the problem is that I dont't get this first printf.
All your values are double for which you have to use %lf. Buut you are using %f which invokes undefined behaviour.
Change %f to %lf in your scanfs and prints.

Resources