Double and Float format displaying different results
Solution 1
In the second example, %d
is used for integers. (d isn't short for double, but for decimal)
scanf ("%d", &number);
should be
scanf ("%lf", &number);
The printf
is incorrect too, as %f
is used for double
printf ("The absolute value of %.2d is %.2d\n", number, absNumber);
Instead, this should be:
printf ("The absolute value of %.2f is %.2f\n", number, absNumber);
Notice that the format specifier for double
is different for scanf
and printf
, this C FAQ article has some good explanations.
Solution 2
In your second program, you have used %d format specifier for double which is wrong. And that's why you are getting error. You should use %lf (long float or better called 'double') for printing it.
For other format specifier,
I think this might help you
a link
You can also search different format specifiers on google by typing 'format specifiers'.
Solution 3
Your last printf
format string is wrong. Should be.
printf ("The absolute value of %.2f is %.2f\n", number, absNumber);
and if you enable all compiler warnings (e.g. compile with gcc -Wall -g
) you'll have a warning about that. Always enable all warnings in the compiler.
Your scanf
are also wrong, should be scanf("%lf", &number);
and with all warnings the compiler would have warned you.
BTW, scanf
returns the "number of successfully matched items" and you should test its result.
You really need to carefully read the documentation of printf(3) and of scanf(3). (In a linux terminal, you could type man 3 printf
to get it).
Notice that arguments are converted to double
and the conversion specifier for double
-s is %f
in printf
and %lf
in scanf
And you should learn how to use a debugger (like gdb
).
Solution 4
Make 2 changes for using double data-type in c :
use %lf in scanf
use %f in printf
//remember : %d is for int
%f is for float
%c is for char
LinhSaysHi
Updated on June 02, 2020Comments
-
LinhSaysHi almost 4 years
I thought that the difference between double and float were the precision of the decimals. However, I am getting strange results with using double and float and they are no where close to one another.
The first segment of code is used with the float format producing the correct results:
#include <stdio.h> #define ABSOLUTE_VALUE(number) ( ((number) < 0) ? -(number) : (number) ) int main (void) { float number, absNumber; printf ("What number do you want to check the absolute value for? : "); scanf ("%f", &number); absNumber = ABSOLUTE_VALUE(number); printf ("The absolute value of %.2f is %.2f\n", number, absNumber); return 0; }
Output:
What number do you want to check the absolute value for? : -3
The absolute value of -3.00 is 3.00The second segment of code is used with the double format producing incorrect results:
#include <stdio.h> #define ABSOLUTE_VALUE(number) ( ((number) < 0) ? -(number) : (number) ) int main (void) { double number, absNumber; printf ("What number do you want to check the absolute value for? : "); scanf ("%d", &number); absNumber = ABSOLUTE_VALUE(number); printf ("The absolute value of %.2d is %.2d\n", number, absNumber); return 0; }
Output:
What number do you want to check the absolute value for? : -3
The absolute value of -03 is 2147344384 -
Basile Starynkevitch over 10 yearsNot
%lf
(this is forlong double
) but%f
-
LinhSaysHi over 10 yearsSo %d is used for integers? Oh darn :( I don't know why I thought it was used for decimals as well
-
Basile Starynkevitch over 10 yearsThis is more a comment than an answer.
-
Yu Hao over 10 yearsWrong for
scanf
,%f
is used forfloat
,%lf
is used fordouble
. Its format is different fromprintf
. My answer is correct. -
Yu Hao over 10 years@BasileStarynkevitch No, in the format of
scanf
,%f
is used forfloat
,%lf
is used fordouble
. Its format is different fromprintf
. -
Jigyasa over 10 yearsbut you can go to the link and can know which format specifier is for which purpose in a more tabulated and easy to read & understandable form.
-
Jigyasa over 10 years@Basile telling him this is not for that etc, I personally feel may confuse him. So, I told what I think is better from my perspective
-
LinhSaysHi over 10 yearsIs a debugger something that will be useful to me in the long run as I keep programming?
-
Basile Starynkevitch over 10 yearsUsing a debugger is in practice absolutely essential when coding, specially in C or C++ ... You really need to learn it...
-
Yu Hao over 10 yearsNo, the format specifier for
double
is different forscanf
andprintf
, see my answer. It's%f
inprintf
. -
Ed Heal over 10 years@LinhSaysHi - I meant in the sense that the format is incorrect. Should have been more explicit
-
Moberg almost 8 years%lf is also available for print f, it is the same as %f in this case so it can be used.