Thread: #define format...

  1. #16
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by citizen
    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.
    http://code-dynasty.net/articles/C_A...ming/83#Macros

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by http://code-dynasty.net/articles/C_And_CPP_Programming/83#Macros
    Many must have heard how "it's better to use const variables than macros" and this is where I say that it's not true. There is really no advantage. The choice is yours. Really.
    Still looking for your point, Desolation.
    A smart compiler might see that a certain variable is const, never modified (through some obscure technique) and is part of the global namespace and decide to optimize it on its own.
    Especially if you make it a global variable.
    Therefore, there is hardly any advantage of using either solution.
    So my choice is just as correct as yours. Macros are also not typesafe, although, this may or may not matter depending on where the macro is used.

  3. #18
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Raigne
    what is the conversion formula from hex to int (just wandering cuzz I just use random hex numbers in my code)
    You seem a mite confused about hex.
    Google some on it, things like "number system", "hex to decimal tutorial", etc.
    Here's a link to start with.

    Basically, decimal, the system you use, is base 10. 10 symbols (the numbers 0 to 9) per digit in the number. Hexadecimal is base 16, so 16 symbols per digit in the number, represented by the numbers 0-9 and letters A-F. The above link will show you how to convert between the two.
    Hexadecimal, binary, and decimal (among others) are all different ways of representing the same values.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    903
    Still looking for your point, Desolation.
    Huhh.. I guess I didn't make it obvious that what I wanted to say is that sometimes you shouldn't just base your coding on what people tell you. Some will tell you this or that is a bad habit. Hell, using goto will get you flamed but it has its uses. I never needed it but if I ever feel I need it, I'll use it.

  5. #20
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> #define JAMES_BOND 007

    Hmmm, before some compiler gave me some hassle about prefexing my #define with 0's

  6. #21
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    having the first character as 0 means you are righting the numer in octo format. So if you did 09 it wouldn't work as there is no symbol 9 in octo.

  7. #22
    Registered User
    Join Date
    May 2006
    Posts
    903
    Yeah it would work.. and yes there is a 9 symbol in octo... 09 would be understood as a base 10 integer and 0x9 is correct.

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why would there be a 9 in octal? It's base eight numbers!

  9. #24
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Simple test:
    Code:
       int value = 09;
    /*
    Error: Illegal octal digit in function main
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    any must have heard how "it's better to use const variables than macros" and this is where I say that it's not true. There is really no advantage. The choice is yours. Really.
    What utter nonsense. Macros are not properly typed, you cannot take their address, and most of all they're not scoped!

    However, in this particular case macros are probably better, because such definitions are usually within a resource header that is also included by the resource compiler, and the resource compiler doesn't understand constants.
    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. 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