C double value not stored using scanf - c

So what is happening is I ask the user for input, then store that into a variable using scanf.
But when I try to retrieve the value, it just a 0.000000. I'm sure I'm just making a stupid noob mistake, but any help you can provide is appreciated.
printf("Enter the radius of your circle/sphere: ");
scanf("%f", &radius);
printf("\n%f", radius);
Ex:
Enter the radius of your circle/sphere: 10
0.0000000
Thanks for your time!

As answered by #Chris McGrath, you must use the %lf format specifier for doubles. However, this doesn't explain why the %f format works just fine for printf.
%lf stands for long float, i.e. double. When values of type float are passed to variadic functions (those accepting a variable number of arguments, such as printf and scanf), they are implicitly promoted to double:
float val;
printf("%f", val); /* printf receives val cast to double, the same
as printf("%f", (double) val) */
printf("%lf", val); /* printf also receives val cast to double, the
same as printf("%lf", (double) val) */
Since both printf("%lf", ...) and printf("%f", ...) end up receiving a double, the two are completely equivalent.
On the other hand, all arguments to scanf are pointers. scanf("%f", ...) expects to receive a float *, and scanf("%lf", ...) a double *:
float floatval;
double dblval;
scanf("%f", &floatval); /* stores a float value to address received */
scanf("%lf", &dblval); /* stores a double value to address received */
The two pointers point to different types, so one cannot be promoted to the other. If they received the same treatment by scanf, it would end up storing the same value into addresses allocated for types of different size, which cannot work.
This is why scanf strictly requires the use of the %lf specifier when used with double values.

As AntonH said, if radius is declared as a double, you must use the %lf format specifier:
scanf("%lf", &radius);

Related

Anyone know why this code always returns 33.80? [duplicate]

Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float?
Example code:
double d;
scanf("%lf", &d);
printf("%f", d);
Because C will promote floats to doubles for functions that take variable arguments. Pointers aren't promoted to anything, so you should be using %lf, %lg or %le (or %la in C99) to read in doubles.
Since С99 the matching between format specifiers and floating-point argument types in C is consistent between printf and scanf. It is
%f for float
%lf for double
%Lf for long double
It just so happens that when arguments of type float are passed as variadic parameters, such arguments are implicitly converted to type double. This is the reason why in printf format specifiers %f and %lf are equivalent and interchangeable. In printf you can "cross-use" %lf with float or %f with double.
But there's no reason to actually do it in practice. Don't use %f to printf arguments of type double. It is a widespread habit born back in C89/90 times, but it is a bad habit. Use %lf in printf for double and keep %f reserved for float arguments.
scanf needs to know the size of the data being pointed at by &d to fill it properly, whereas variadic functions promote floats to doubles (not entirely sure why), so printf is always getting a double.
Because otherwise scanf will think you are passing a pointer to a float which is a smaller size than a double, and it will return an incorrect value.
Using either a float or a double value in a C expression will result in a value that is a double anyway, so printf can't tell the difference. Whereas a pointer to a double has to be explicitly signalled to scanf as distinct from a pointer to float, because what the pointer points to is what matters.

Strange behavior when reading and writing "float" and "double" types in C [duplicate]

Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float?
Example code:
double d;
scanf("%lf", &d);
printf("%f", d);
Because C will promote floats to doubles for functions that take variable arguments. Pointers aren't promoted to anything, so you should be using %lf, %lg or %le (or %la in C99) to read in doubles.
Since С99 the matching between format specifiers and floating-point argument types in C is consistent between printf and scanf. It is
%f for float
%lf for double
%Lf for long double
It just so happens that when arguments of type float are passed as variadic parameters, such arguments are implicitly converted to type double. This is the reason why in printf format specifiers %f and %lf are equivalent and interchangeable. In printf you can "cross-use" %lf with float or %f with double.
But there's no reason to actually do it in practice. Don't use %f to printf arguments of type double. It is a widespread habit born back in C89/90 times, but it is a bad habit. Use %lf in printf for double and keep %f reserved for float arguments.
scanf needs to know the size of the data being pointed at by &d to fill it properly, whereas variadic functions promote floats to doubles (not entirely sure why), so printf is always getting a double.
Because otherwise scanf will think you are passing a pointer to a float which is a smaller size than a double, and it will return an incorrect value.
Using either a float or a double value in a C expression will result in a value that is a double anyway, so printf can't tell the difference. Whereas a pointer to a double has to be explicitly signalled to scanf as distinct from a pointer to float, because what the pointer points to is what matters.

Different float input and output

float amount;
printf("Enter the amount:\n");
scanf("%f", &amount);
// input: 100.10
printf("%f", amount);
Output: 100.099998
The problem is that the output is not 100.10 same as the input;
Floating point numbers like 100.10 has no exact binary representations. That's why you encountered rounding error.
Note that there is something more behind, %f in printf actually expects a double argument, so here amount is converted to double in the printf call.
The reason is, variable argument functions like printf always promote their variable argument parts, that's why printf has no format specifier for float, because it always sees double. So a better program to demonstrate your question is to use double instead:
double amount;
printf("Enter the amount:\n");
scanf("%lf", &amount);
printf("%f", amount);
You'll still get rounding error, but no conversion from float to double is done. And to demonstrate the program, you may need to print more digits as double is more accurate.

how to keep decimals in c

I am writing simple code for homework.
I get one number from user, which is 3.4, when I define it with
scanf("%d",&a)
it takes only 3 and do it that way. I defined a as
int a;
What should I do?
I think you are very new to c programming. This is a very simple job. This can be done as:-
float a;
// to input a data in the variable a
scanf("%f",&a);
//to display the stored data
printf("%f\n",a);
//or
printf("%.nf\n",a);//specify value of n
//maximum value of n is 6 also is its default value
//for e.g. printf("%.2f",a); will display decimal number upto two digits after the decimal place
//you can know more about displaying data as
%d: decimal value (int)
%c: character (char)
%s: string (const char *)
%u: unsigned decimal value (unsigned int)
%ld: long int (long)
%f: float value (float or double)
%x: decimal value in Hexadecimal format
%o: decimal value in octal format
For avialabe list of formatting specifications go here
Use float
float a;
scanf("%f", &a);
%d is for int. 3.4 is not an int type, you can use float.
float x;
scanf("%f", &x);
For detailed information about data types, you can check here: http://en.wikipedia.org/wiki/C_data_types
And also here: http://www.techonthenet.com/c_language/variables/index.php
You should define a as
float a;
and replace %d with %f in scanf
scanf("%f", &a);
What should I do?
Read Basics of Formatted Input/Output in C.
Its very simple for int variable we use %d format specifier and if we want float we use %f format specifier. Because according to IEEE int and float bit map format are different.
You are declaring int a which can take only integer value so what you have to do is make it float which is used for numbers having decimal point
float a;
scanf(%f,&a);

Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

Why is it that scanf() needs the l in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a float?
Example code:
double d;
scanf("%lf", &d);
printf("%f", d);
Because C will promote floats to doubles for functions that take variable arguments. Pointers aren't promoted to anything, so you should be using %lf, %lg or %le (or %la in C99) to read in doubles.
Since С99 the matching between format specifiers and floating-point argument types in C is consistent between printf and scanf. It is
%f for float
%lf for double
%Lf for long double
It just so happens that when arguments of type float are passed as variadic parameters, such arguments are implicitly converted to type double. This is the reason why in printf format specifiers %f and %lf are equivalent and interchangeable. In printf you can "cross-use" %lf with float or %f with double.
But there's no reason to actually do it in practice. Don't use %f to printf arguments of type double. It is a widespread habit born back in C89/90 times, but it is a bad habit. Use %lf in printf for double and keep %f reserved for float arguments.
scanf needs to know the size of the data being pointed at by &d to fill it properly, whereas variadic functions promote floats to doubles (not entirely sure why), so printf is always getting a double.
Because otherwise scanf will think you are passing a pointer to a float which is a smaller size than a double, and it will return an incorrect value.
Using either a float or a double value in a C expression will result in a value that is a double anyway, so printf can't tell the difference. Whereas a pointer to a double has to be explicitly signalled to scanf as distinct from a pointer to float, because what the pointer points to is what matters.

Resources