unsigned long long overflow?

D

dmpk2k

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
 
K

Kevin Easton

dmpk2k said:
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.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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"
 
B

Bill Hanna

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
 
B

Bill Hanna

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
 
E

Emmanuel Delahaye

In said:
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top