473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

unsigned long long overflow?

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 5682
dmpk2k <dm******@myahoo.com> 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
In <9X*******************@news04.bloor.is.net.cable.r ogers.com> Tom St Denis <to********@iahu.ca> 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
In 'comp.lang.c', dmpk2k <dm******@Myahoo.com> 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
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #4
In <Xn**************************@130.133.1.4> Emmanuel Delahaye <em**********@noos.fr> writes:
In 'comp.lang.c', dmpk2k <dm******@Myahoo.com> 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
Da*****@cern.ch (Dan Pop) wrote in message news:<be**********@sunnews.cern.ch>...
In <Xn**************************@130.133.1.4> Emmanuel Delahaye <em**********@noos.fr> writes:
In 'comp.lang.c', dmpk2k <dm******@Myahoo.com> 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
Da*****@cern.ch (Dan Pop) wrote in message news:<be**********@sunnews.cern.ch>...
In <Xn**************************@130.133.1.4> Emmanuel Delahaye <em**********@noos.fr> writes:
In 'comp.lang.c', dmpk2k <dm******@Myahoo.com> 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
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
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
49
by: Neil Zanella | last post by:
Hello, Often I happen to be dealing with nonnegative integers and since I know I won't need negative numbers here I declare them as unsigned simply to make the program somewhat clearer....
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
3
by: Mike Miller | last post by:
What is the best way to convert a managed unsigned int64 to an unsigned long? Basically I need to do the following: System::UInt64 managedInt = 10; unsigned long unmanagedInt; unmanagedInt =...
10
by: jeff | last post by:
unsigned long a = ...; long b = (long)a; while a overflows, what is the result of b? Thank you.
3
by: linq936 | last post by:
I have some algorithm dealing with unsigned int calculation a lot, I am trying to add some check for overflow. The initial thinking was very easy, just do something like this, int...
26
by: John Harrison | last post by:
I have a problem. I want to compare an integral value, n, against three half open ranges as follows [-A, 0) // range 1 [0, B) // range 2 [B, C} // range 3 Each range corresponds to a...
17
by: Tarique | last post by:
This program was compiled on MS Visual C++ 08 /*Fibonacci Numbers*/ #include<stdio.h> #include<limits.h> void fibonacci(int n) { unsigned long long fib0 = 0; /*First Fibonacci Number*/
105
by: Keith Thompson | last post by:
pereges <Broli00@gmail.comwrites: These types already have perfectly good names already. Why give them new ones? If you must rename them for some reason, use typedefs, not macros. --
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.