Thread: #define format...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    #define format...

    when people do...

    #define MENUITEM1 0x0401

    this is really an integer right?
    so why don't people just do...

    #define MENUITEM1 101

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    0x0401 is a hexadecimal number (1,025 in decimal)

    I can't think of a good reason in that case to use hex, but if you wanted to define, for example, a bitmask such as:
    Code:
    #define MASK 0xFFFF //1111 1111 1111 1111 binary
    //as opposed to
    #define MASK 65535
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Who knows. Maybe it just looks 1337. Maybe it's stupid repellant: like,

    #define MENUITEM1 101
    J. Random Luser: "Oh look! A number! I'll just change it so my hack works."

    #define MENUITEM 0x0401
    J. Random Luser: "Oh crap... that must do something important. Better not mess with it. "
    *original programmer smiles*

    Or maybe it was used in context with other hex numbers where it would suck if you changed bases randomly via macro.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Certain features of the Win32 API, for example, requires you to use values in a certain range. Often, these ranges are given in hex numbers.

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    of course the best one is

    Code:
    #define SOME_NUM 0x05 // dec 5 == hex 5, it's the same freakin number!!!!


    although as citizen says, if you have a bunch of other valid hex values it's easier to keep consistency...
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Nah, sometimes we need more action:
    Code:
    #define JAMES_BOND 007
    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

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by laserlight
    Nah, sometimes we need more action:
    Code:
    #define JAMES_BOND 007
    yes, but macros are EVIL! look, I'll prove it!

    Code:
    #define MACRO_OF_THE_BEAST 0x29A // no points for guessing this in decimal
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I personally go with
    Code:
    #define QUESTION  2B || !2B
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I use hex in my constants for the simple reason that I am decoding a binary file and over half the numbers I am working with are hex and it is easier to keep track of them in my head if I am reading them all in hex than to try and read some in decimal some in hex...

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    what is the conversion formula from hex to int (just wandering cuzz I just use random hex numbers in my code)

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Conversion from hex to integer does not make sense. Hex is not a data type but a numbering system.

    Binary - base 2
    Octal - base 8
    Decimal - base 10
    Hex - base 16

    So hex, binary, decimal, octal all have integers, just represented differently.

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    so really the compiler distinguishes the macro definition as an integer anyway... decimal would be good for a menu_id?

    #define MENU1 101
    #define MENU2 102
    #define MENU3 103

    or is it just as easy saying ...

    #define MENU1 0x01
    #define MENU2 0x02
    #define MENU3 0x03

    ORRRRRRR does it just depend on programming styles of each programmer?

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    101 and 0x01 are not the same... 101 and 0x65 are the same... sometimes it is easier to work with hex numbers, but most of the time you are better off with decimal

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    First, if you are writing this you need to decide if the macro is truly necessary, because it probably isn't.
    Code:
    const int MENUITEM1 = 1;
    const int MENUITEM2 = 2;
    const int MENUITEMn = n; ...
    Even if it is necessary, write your constant in whatever base you want as long as you don't confuse anyone who will read your code, and be consistant with the base you choose. Don't define a hex number and then use it with decimals.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    903
    Chances are that simple base 10 numbers will be sufficient for your needs. If you need to register your own messages, though, you have to put them in a range of IDs specified in hex numbers.

    WM_APP

    Range Description
    From 0 through WM_USER –1 Messages reserved for use by the system.
    From WM_USER through 0x7FFF Integer messages for use by private window classes.
    WM_APP through 0xBFFF Messages available for use by applications.
    0xC000 through 0xFFFF String messages for use by applications.
    Greater than 0xFFFF Reserved by the system for future use.
    I use this in one of my applications.
    Last edited by Desolation; 06-27-2006 at 10:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  3. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  4. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  5. queued signals problem, C programming related
    By Unregistered in forum Linux Programming
    Replies: 3
    Last Post: 01-22-2002, 12:30 AM