Hi All,
This may seem trivial but I have totally no idea.
Question: Why does the following code fragment fail to work?
Many Thanks!Code:int a = 1000, b = 1000; long int c; c = a * b;
Hi All,
This may seem trivial but I have totally no idea.
Question: Why does the following code fragment fail to work?
Many Thanks!Code:int a = 1000, b = 1000; long int c; c = a * b;
Think of the possible range of int and long int, and the result of the multiplication.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What do you mean by "fail to work"?
It looks like it should work fine unless you're using a 16-bit environment or something.
If you understand what you're doing, you're not learning anything.
It's a dumb exam question.
Int 32,767.
Long Int at least 4 billion for International ASCII Standard.
So. Why why?! What a lame exam qn.
Obviously, because the result of the multiplication might not fit into the range of int.Originally Posted by Inneart
I disagree. It is actually useful to get you to consider the limitations of what you are dealing with.Originally Posted by Inneart
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why? Simple answer: Because 1,000,000 doesn't fit into a 16-bit integer. If you want it to work, simply cast a and b to long integers in the multiplication.
If you understand what you're doing, you're not learning anything.
Hmm. So you guys are saying irregardless of the fact that c is being declared as long int, as long as a and b are just int, int multiply by int will force c to adopt int class?
No. The result of the multiplication is of type int, and this result is then assigned to the long int variable.Originally Posted by Inneart
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I see. I didn't know that a simple expression is so complicated. So as long as 1000*1000 is > int's range, even if I declare c as long int the compiler will 'jammed' at the part when 1000 * 1000 = 1,000,000?
Maybe you should read up on implicit type conversion in C.
If you understand what you're doing, you're not learning anything.
Sorry for being an amateur but I thought a long int is capable of holding up to 32-bit results? So why is a 16-but result defaulting the process?
Let's introduce another variable:Originally Posted by Inneart
Code:int a = 1000, b = 1000; long int c; int t; /* assume INT_MAX is 32767 or 32768 */ t = a * b; c = t;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ok... I hope nobody minds if I take a stab at this one....
You have two 16 bit ints defined ... in C the result is going to be a 16 bit int, unless you tell it otherwise. What you need here is a type cast to tell the compiler what flavour of result you want...
You're working in a language that assumes nothing... you have to tell it everything.Code:int a = 1000; int b = 1000; long int c; c = (long int) a * b;
I know this question is already answered, but I agree with laserlight, this is NOT a dumb question and there are two reasons why:
a) No question is a dumb question.
b) This questions touches on the idea of overflow and type range limitations. This concept is VERY IMPORTANT in the embedded world where micro-controllers are often limited in their data and address bus widths.
The data types in all programming languages are affected by the underlying architecture in terms of their size.