0

I don't know what is causing this error, I try dividing 2/12 using double but it gives me a completely wrong number.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double a;
    a = 2/12;
    printf("\n a = %lf  \n", a);
    return 0;
}

This returns -272632568 and makes no sense. Thanks for your help.

  • What compiler and flags? – txk2048 May 17 '20 at 16:25
  • I'm using Xcode and I've tried with other compilers, including online ones, but it doesn't work. – Rolando González May 17 '20 at 16:26
  • 1
    `%d` is for integers, use `%f`. Your compiler could've warned you about this, consider enabling the warnings. Also note that `2/12` does integral division and truncates the result, add `.` to at least one of the operands to fix that: `2/12.0`. – HolyBlackCat May 17 '20 at 16:27
  • You have undefined behaviour, spotted by MSVC: **warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'double'** – Adrian Mole May 17 '20 at 16:27
  • But even correcting the format to `%f`, remember that `2 / 12` is performed as **integer division** (giving zero) - only afterwards is it converted to a double. – Adrian Mole May 17 '20 at 16:29
  • 1
    Anyone want to guess what the next question the OP asks is going to be, if they show the same willingness to do research first as for this question? – EOF May 17 '20 at 16:30
  • My compiler prints the correct answer (which is 0, not 0.166666... as you would expect, because you are doing integer division). Is this the exact code you compiled when computing that non-sense answer? – DarkAtom May 17 '20 at 16:44
  • Does this answer your question? [Division result is always zero](https://stackoverflow.com/questions/2345902/division-result-is-always-zero) – phuclv May 17 '20 at 16:51
  • @phuclv That still doesn't explain the result that the OP got (-272632568) – DarkAtom May 17 '20 at 16:54
  • 1
    The format specifier `%lf` is unspecified and gives undefined behavior (the `l` modifier can only be applied to int formats). Some libraries may treat it as `long double`. – Chris Dodd May 17 '20 at 17:55
  • @ChrisDodd `%lf` means `double` argument. For `long double` use `%Lf`. This is standard, not a compiler extension. Who told you that the `l` modifier can only be applied to `int` formats? It can be applied even to chars or strings to make them wide. – DarkAtom May 18 '20 at 09:47
  • @DarkAtom: The spec told me -- `%f` means a `double` argument, `%Lf` means `long double`. `%lf` is unspecified, so undefined behavior. Wide chars/strings is a recent extension, not part of the original C89 spec. – Chris Dodd May 19 '20 at 02:45
  • I was talking about the C99 standard and later. In no way is the question marked C89 specific. – DarkAtom May 19 '20 at 08:19

2 Answers2

1

First of all, you should consider the declaration:

double a = 2 / 12; // (integer)

It'll just give you zero and then assign to double since they're integers. If you use rather:

double a = 2.0 / 12.0; // explicitly defining

Then you'll get right precision output by using this statement:

printf("\n a = %f\n", a);

Or,

printf("\n a = %lf\n", a);
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
0

You used %d in your format string, however %d means integer not double.

You want to use %f inplace of %d

See here for details: https://www.gnu.org/software/libc/manual/html_node/Table-of-Output-Conversions.html#Table-of-Output-Conversions

txk2048
  • 281
  • 3
  • 15