unsigned long long in C

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
anak_papua
New poster
Posts: 7
Joined: Wed Mar 19, 2003 3:16 pm

unsigned long long in C

Post 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.
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

Your usage is good. Strange, but I couldn't repeat this behaviour ....
Are you using Windows or Unix ?

Dominik
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post by turuthok »

Try to initialize your a to 0 and see if you still see the same behavior.

-turuthok-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
turuthok
Experienced poster
Posts: 193
Joined: Thu Sep 19, 2002 6:39 am
Location: Indonesia
Contact:

Post 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-
The fear of the LORD is the beginning of knowledge (Proverbs 1:7).
anak_papua
New poster
Posts: 7
Joined: Wed Mar 19, 2003 3:16 pm

Post 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
Viktoras Jucikas
New poster
Posts: 22
Joined: Sun Oct 20, 2002 6:41 pm
Location: Lithuania
Contact:

Post 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).
Yarin
Problemsetter
Posts: 112
Joined: Tue Sep 10, 2002 5:06 am
Location: Ume
Contact:

Post 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.
bery olivier
Learning poster
Posts: 90
Joined: Sat Feb 15, 2003 1:39 am
Location: Paris, France
Contact:

about (unsigned) long long

Post 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
Not AC yet Image AC at last Image
Post Reply

Return to “C”