Problem Code: Inline

This is a discussion on Problem Code: Inline within the C++ Programming forums, part of the General Programming Boards category; Ok, first post, I know I'm not allowed to ask for someone to do my homework for me but I've ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Unhappy Problem Code: Inline

    Ok, first post, I know I'm not allowed to ask for someone to do my homework for me but I've looked everywhere for a possible solution to this peice of problem code and I cant even find a complier that replicates the error let alone a mention of what I should be looking for:

    It's an inline int Max(a, b) function thats given 10, and 0 as

    Code:
    #include <stdio.h>
    typedef unsigned short Word;
    inline int
    Max( int n, int m)
    {
         return n>m ? n : m;
    }
    Word w1=40000,w2 = 0;
    int
    main()
    {
         printf( “%d\n”, Max( w1, w2) );
         return 0;
    }
    The question says that sometimes this code will return 0, I cant find a complier that does that.

    Any help just with what errors i should be looking for would be greatly appricated.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    My only guess is that it's something to do with the usage of short, do some compilers not recognise the unsigned property? If that were true the 40000 value would be greater than the positive limit for a short and the program would return 0...or error.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    It's not guaranteed that 'int' will be thirty-two bits wide. Then the arguments to Max could be, on some systems, 16 bit signed integers, making Max receive w1 as a negative value (since it's greater than 2^31 - 1).
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOh.


    Genius. Thanks very much

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by CodingFleet View Post
    Code:
         return 0;
    The question says that sometimes this code will return 0, I cant find a complier that does that.
    I found where it returns zero!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Rashakil Fol View Post
    It's not guaranteed that 'int' will be thirty-two bits wide. Then the arguments to Max could be, on some systems, 16 bit signed integers, making Max receive w1 as a negative value (since it's greater than 2^31 - 1).
    Picking lint: should say (since it's greater than 2^15- 1).

    So a compiler with 16-bit ints will see 40000 as a negative number because the function Max isn't using unsigned. A good compiler with warnings enabled should also warn about mixing signed and unsigned.

    --
    Mats

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If you want to try out such a compiler, see if you can find really old versions of Turbo C.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  2. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 02:37 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM

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