Thread: Theory QN Help!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Think of the possible range of int and long int, and the result of the multiplication.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    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. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    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. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Inneart View Post
    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.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Inneart View Post
    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.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    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. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Inneart View Post
    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. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 08:37 AM
  2. MMO Theory, How to make Gameloop & Sockets both work?
    By Zeusbwr in forum Game Programming
    Replies: 3
    Last Post: 08-01-2005, 12:29 PM
  3. Graph Theory
    By xds4lx in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-17-2002, 12:58 PM
  4. Set Theory
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2002, 08:02 AM
  5. Music Theory [A Formal Post]
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-06-2001, 12:20 AM