\nWhat number do you want to check the absolute value for? : -3
\nThe absolute value of -3.00 is 3.00
The second segment of code is used with the double format producing incorrect results:
\n\n#include <stdio.h>\n\n#define ABSOLUTE_VALUE(number) ( ((number) < 0) ? -(number) : (number) )\n\nint main (void)\n{\n double number, absNumber;\n\n\n printf (\"What number do you want to check the absolute value for? : \");\n scanf (\"%d\", &number);\n\n absNumber = ABSOLUTE_VALUE(number);\n\n printf (\"The absolute value of %.2d is %.2d\\n\", number, absNumber);\n\n return 0;\n}\n
\n\nOutput:
\nWhat number do you want to check the absolute value for? : -3
\nThe absolute value of -03 is 2147344384
In the second example, %d
is used for integers. (d isn't short for double, but for decimal)
scanf (\"%d\", &number);\n
\n\nshould be
\n\nscanf (\"%lf\", &number);\n
\n\nThe printf
is incorrect too, as %f
is used for double
printf (\"The absolute value of %.2d is %.2d\\n\", number, absNumber);\n
\n\nInstead, this should be:
\n\nprintf (\"The absolute value of %.2f is %.2f\\n\", number, absNumber);\n
\n\nNotice that the format specifier for double
is different for scanf
and printf
, this C FAQ article has some good explanations.
Reputation: 642
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.00
The 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
Upvotes: 2
Views: 46538
Reputation: 122493
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.
Upvotes: 15
Reputation: 186
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'.
Upvotes: 2
Reputation: 1
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
).
Upvotes: 2