Page 1 of 1

unsigned long long in C

Posted: Thu Mar 20, 2003 4:57 am
by anak_papua
how to use unsigned long long in C? was my syntax error?
[c]
#include <stdio.h>

int main() {
unsigned long long a;

scanf ("%llu", &a);

printf ("%llu", a);
return 0;
}
[/c]

input :
1234567890

output :
2883627802384794322

how can this happened?

thanks before.

Posted: Thu Mar 20, 2003 8:59 am
by Dominik Michniewski
Your usage is good. Strange, but I couldn't repeat this behaviour ....
Are you using Windows or Unix ?

Dominik

Posted: Thu Mar 20, 2003 9:38 am
by turuthok
Try to initialize your a to 0 and see if you still see the same behavior.

-turuthok-

Posted: Thu Mar 20, 2003 9:42 am
by turuthok
The lower 32-bits of 2883627802384794322 is indeed 1234567890 ... I wonder why scanf() doesn't overwrite the upper 32-bits for this case ...

Something new everyday :)

-turuthok-

Posted: Thu Mar 20, 2003 10:05 am
by anak_papua
aha.. i should initialize first, so that the output gave the same result, thank you :) :)
however i dont understand why we should initialize first, perhaps bugs?
i compiled it with GCC 2.95.3, FreeBSD 4.7 :)
once again thank you
:D :D :D :D :D :D

Posted: Thu Mar 20, 2003 11:16 am
by Viktoras Jucikas
long long is not ANSI C type first of all, however most compilers support it (though support to GNU libc was added quite recently). Your problem might be the ll flag. Quoting scanf man page:
The q flag is the BSD 4.4 notation for long long, while ll or the usage of L in integer conversions is the GNU notation.

Initializing to 0 is not the solution as 1234567890 is a 32bit number, if you try with larger ones, it should behave incorrectly (upper 32bits will not be set probably).

The simple and straightforward way I have avoided it was by using C++ streams for input/output (though long long is not ANSI C++ either).

Posted: Thu Mar 20, 2003 12:00 pm
by Yarin
Yes, the short, easy answer is that scanf (at least in GCC) simply doesn't work with %ll or %llu, you have to use C++ cin, or parse the long long yourself as a string.

about (unsigned) long long

Posted: Fri Mar 21, 2003 1:50 pm
by bery olivier
The online judge supports long long int. If your compiler doesn't you can write your code whit long int and test it with small number. Before send your code, you only need to put something like : [c]#define long long long int[/c]
and replace all your %ld or %lu by %lld or %llu