-
Scanf and long long
From what I've just tested now and from scouring the net it appears that scanf is broken when it comes to "long long" types. Scanning in a long long using "%lld" and "%ld" crashed for me. I'm using MingW on windows. Does anyone know if this is fixed in the latest version of GCC?
-
MinGW uses the microsoft run-time, so you need to use the microsoft specific conversion, not the ANSI conversion.
It's something like "%I64d", but check the relevant MSDN page.
-
Yep, you're right, using "%I64d" works now. This raises two further questions though:
Does Microsoft officially support a "long long" type? I.E. should "__int64" be used instead of "long long" when programming for windows (since "long long" by ISO standards is not garaunteed to be just 64bits)?
And if so, how does Microsoft then handle an "unsigned long long" type for scanf/printf? "%uI64" or "%uI64d" etc do not work.. here is some code that tests some of these issues:
Code:
#include <stdio.h>
#include <limits.h>
int main(void)
{
long long int test;
printf("Enter long long int:\n");
scanf("%I64d", &test);
printf("%I64d", test);
printf("\n%I64d", LLONG_MAX);
printf("\n%ull", ULLONG_MAX);
printf("\n%uI64", ULLONG_MAX);
}
The last two produce interesting results...
-
> "%uI64" or "%uI64d" etc do not work
I64 is a prefix, like ll, so it would need to be "%I64u"
> Does Microsoft officially support a "long long" type? I.E. should "__int64" be used instead of "long long" when programming for windows
> (since "long long" by ISO standards is not garaunteed to be just 64bits)?
Well the relationship between long long and __int64 is something only the maintainers of MinGW have to worry about. For the most part, long long will do as you expect.
It's basically a "bug" in MinGW in relying on a non-C99 compliant library to implement C99 features.
Though if you're calling some windows specific API which uses __int64, then you should probably use it.
Interestingly, your man page URL indicates that "ll" is a valid prefix. I guess that at some point that the MinGW maintainers will decide to link with a later version of msvcrd.dll.
You might be able to do this manually, by editing some option. I don't know, I've never tried it.