-2

I am trying to multiply four integers into a __int64. The value it should have is 4096 * 1024 * 64 * 64 = 17,179,869,184. This is too large of a number to store in an int, thats why I'm using __int64. The output I get however is 0.

I've tried casting every single one of the integers to a __int64, which doesnt change the output from being 0. Also, if I check with a debugger, the value of test is in fact 0, so it is not a problem with outputting it.

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

int main() {
    __int64 test = (__int64)4096 * (__int64)1024 * (__int64)64 * (__int64)64;
    printf("%d", test);
    getchar();
}

The output:

0

The expected output:

17179869184

2 Answers2

0

The calculation is probably OK, try changing your output format to long form, so :

printf("%lld", test);

EDIT: should cast type as well, so :

printf("%lld", (long long)test);
racraman
  • 4,988
  • 1
  • 16
  • 16
  • It should be `long long` then, `%lld`, and `test` should be cast to `long long`. – Ruslan Aug 15 '19 at 06:34
  • I have mentioned in my question that I have checked with the VS2015 debugger that the value of test is in fact 0. Printing with ```%ld``` does not change the output. Also, calculations performed with ```test``` result in wrong results, so it does have a wrong value. – Niels Slotboom Aug 15 '19 at 06:35
  • 1
    @NielsSlotboom no, it's not zero, it's 0x400000000. See [the assembly output from MSVC](https://gcc.godbolt.org/z/G0ayW6). You just have to use the correct type specifier and make sure that the type passed is correct. – Ruslan Aug 15 '19 at 06:42
  • I have realized a mistake. In my project I'm working on (so not the sample code above) I'm not casting the values to ```__int64```. Thats why checking with the VS2015 debugger does show the value to be ```0```, because it actually gets computed wrong. In the sample code I do cast the values, so it computes correctly. If you could edit your answer to say ```%lld``` insted of ```%ld``` then I will accept it. – Niels Slotboom Aug 15 '19 at 06:43
  • `warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long long int’ [-Wformat=]`, `i686-w64-mingw32-gcc` suggest `%I64d` – Alex Aug 15 '19 at 06:51
  • Better one there: [How to print a int64_t type in C](https://stackoverflow.com/a/9225648/2498790) – Alex Aug 15 '19 at 06:58
0

Your printf format specifier is wrong: %d is for int, whereas the argument is __int64.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I have mentioned in my question that I have checked with the VS2015 debugger that the value of ```test``` is in fact 0. Printing with ```%ld``` does not change the output. – Niels Slotboom Aug 15 '19 at 06:34