1. ## Theory QN Help!

Hi All,

This may seem trivial but I have totally no idea.

Question: Why does the following code fragment fail to work?

Code:
```int a = 1000, b = 1000;
long int c;

c = a * b;```
Many Thanks!

2. Think of the possible range of int and long int, and the result of the multiplication.

3. 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.

4. 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.

5. Originally Posted by Inneart
Int 32,767.
Long Int at least 4 billion for International ASCII Standard.

So. Why why?!
Obviously, because the result of the multiplication might not fit into the range of int.

Originally Posted by Inneart
What a lame exam qn.
I disagree. It is actually useful to get you to consider the limitations of what you are dealing with.

6. 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.

7. 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?

8. Originally Posted by Inneart
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.

9. 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?

10. Originally Posted by Inneart
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?
Maybe you should read up on implicit type conversion in C.

11. Originally Posted by Inneart
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?
There's no jamming involved. Two ints are multiplied together with a 16-bit result. That 16-bit result is then assigned to the long int c. It's the 16-bit result part that's hanging you up.

12. 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?

13. Originally Posted by Inneart
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:
Code:
```int a = 1000, b = 1000;
long int c;
int t; /* assume INT_MAX is 32767 or 32768 */
t = a * b;
c = t;```

14. Originally Posted by Inneart
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?
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...

Code:
```int a = 1000;
int b = 1000;
long int c;

c = (long int) a * b;```
You're working in a language that assumes nothing... you have to tell it everything.

15. 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.