454,443 Members | 1,409 Online
Need help? Post your question and get tips & solutions from a community of 454,443 IT Pros & Developers. It's quick & easy.

# unsigned long long overflow?

 P: n/a Forgive me if this is obvious to you. I feel somewhat foolish. I've scribbled together something that demonstrates my problem. It takes the product of all integers n squared, where n is 1..10. ----------------- int main() { int i; unsigned long long product = 1; for (i=1; i<=10; i++) product *= i * i; printf("%U\n", product); return 0; } ----------------- Would someone care to inform me why the output is 4114677760 and not 13168189440000? sizeof() tells me that I'm getting a 64-bit variable, so what's going on? Is it an issue with printf()? And how do I resolve this? Thanks, dmpk2k Nov 13 '05 #1
7 Replies

 P: n/a dmpk2k wrote: Forgive me if this is obvious to you. I feel somewhat foolish. I've scribbled together something that demonstrates my problem. It takes the product of all integers n squared, where n is 1..10. ----------------- int main() { int i; unsigned long long product = 1; for (i=1; i<=10; i++) product *= i * i; printf("%U\n", product); printf("%llu\n", product); return 0; } ----------------- - Kevin. Nov 13 '05 #2

 P: n/a In <9X*******************@news04.bloor.is.net.cable.r ogers.com> Tom St Denis writes: dmpk2k wrote: Forgive me if this is obvious to you. I feel somewhat foolish. I've scribbled together something that demonstrates my problem. It takes the product of all integers n squared, where n is 1..10. ----------------- int main() { int i; unsigned long long product = 1; for (i=1; i<=10; i++) product *= i * i; printf("%U\n", product);%U is wrong for long long %U is wrong for *anything*, being plain undefined behaviour. Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 13 '05 #3

 P: n/a In 'comp.lang.c', dmpk2k wrote: int main() { int i; unsigned long long product = 1; for (i=1; i<=10; i++) product *= i * i; printf("%U\n", product); You want "%llu" return 0; } -- -ed- em**********@noos.fr [remove YOURBRA before answering me] The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ Nov 13 '05 #4

 P: n/a In Emmanuel Delahaye writes: In 'comp.lang.c', dmpk2k wrote: int main() { int i; unsigned long long product = 1; for (i=1; i<=10; i++) product *= i * i; printf("%U\n", product);You want "%llu" Unless he has a conforming C99 implementation, there is no way of telling what he really needs. Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 13 '05 #5

 P: n/a Da*****@cern.ch (Dan Pop) wrote in message news:... In Emmanuel Delahaye writes:In 'comp.lang.c', dmpk2k wrote: int main() { int i; unsigned long long product = 1; for (i=1; i<=10; i++) product *= i * i; printf("%U\n", product);You want "%llu" Unless he has a conforming C99 implementation, there is no way of telling what he really needs. Dan MS Visual C++ and Bloodshed C++ have a bug in the ostream program that limits the output to 32 Bits when using printf. It will only output the lower half of the long long data. MS has listed a work-around fix on their website. I have tried to use their recommendation but it does not work. MS states that the bug will be fixed in Visual C.net. Bill Hanna Nov 13 '05 #6

 P: n/a Da*****@cern.ch (Dan Pop) wrote in message news:... In Emmanuel Delahaye writes:In 'comp.lang.c', dmpk2k wrote: int main() { int i; unsigned long long product = 1; for (i=1; i<=10; i++) product *= i * i; printf("%U\n", product);You want "%llu" Unless he has a conforming C99 implementation, there is no way of telling what he really needs. Dan Use "%I64d" or "%I64u" or "%I64x". This works on Bloodshed C++. I found the info from an old posting in 2001. Bill Hanna Nov 13 '05 #7

 P: n/a In 'comp.lang.c', bi*****@aol.com (Bill Hanna) wrote: Use "%I64d" or "%I64u" or "%I64x". This works on Bloodshed C++. I found the info from an old posting in 2001. The Dev-C++ IDE 4.x by Bloodshed uses gcc 3.2.x. It supports C99 'long long' and "%llu". -- -ed- em**********@noos.fr [remove YOURBRA before answering me] The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ Nov 13 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion.