Theory QN Help!

This is a discussion on Theory QN Help! within the C Programming forums, part of the General Programming Boards category; Hi All, This may seem trivial but I have totally no idea. Question: Why does the following code fragment fail ...

  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
    21,734
    Think of the possible range of int and long int, and the result of the multiplication.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,164
    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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,164
    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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,164
    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,164
    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
    21,734
    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;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 1 of 2 12 LastLast
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, 07: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, 11:58 AM
  4. Set Theory
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2002, 07:02 AM
  5. Music Theory [A Formal Post]
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-05-2001, 11:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21