LinhSaysHi
LinhSaysHi

Reputation: 642

Double and Float format displaying different results

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

Answers (4)

Yu Hao
Yu Hao

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 %fis 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

Jigyasa
Jigyasa

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

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

Ed Heal
Ed Heal

Reputation: 60037

If you read the manual, "%d" is for integers. You need "%lf" instead.

Ditto with the printf

Upvotes: 0

Related Questions