How the hell do you write macros?

This is a discussion on How the hell do you write macros? within the C++ Programming forums, part of the General Programming Boards category; How do you write macros, and how do they work?...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    How the hell do you write macros?

    How do you write macros, and how do they work?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Like a normal function, sort of:
    Code:
    #define PowerOfTwo(n) (n * n)
    They have to be written in one single line, or use \ at the endof a line to break it into multiple lines.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Would it be possible to write most of a program using only pre-processing directives and marcos?

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Macros directly modify your code, and hence, can be dangerous if not used properly. For example, let's take a look at a use of the macro provided above:
    Code:
    #define SQUARE(x) (x * x)
    
    int main( )
    {
       int y = 3;
       int z = SQUARE(y + 1);
    }
    Now, clearly, the intended meaning is to assign z = (y + 1) * (y + 1) = 4 * 4 = 16. However, there is a direct substitution done, so what happens is: z = y + 1 * y + 1 = 2 * y + 1 = 7. This could be fixed as follows:
    Code:
    #define SQUARE(x) ((x) * (x))
    ... but it demonstrates the danger. As to writing a whole program using them... well, you have to have actual code in the macros themselves, and all you are going to do is make a mess, so unless you are writing code for say, the International Obfuscated C Code Contest, there is absolutely no point.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by Finchie_88
    Would it be possible to write most of a program using only pre-processing directives and marcos?
    Yes, but why would you want to?
    My best code is written with the delete key.

  6. #6
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    #define GREATER(x,y)(x>y?x:y)

    Like that.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A note... If you do write macros, enclose all uses of your parameters in parentheses:
    Code:
    #define GREATER(x,y)((x)>(y)?(x):(y))
    As I pointed out above, operator precedence can be a real pain dealing with macros, so make sure that you don't get any side effects.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 07:52 PM
  2. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  3. program to make a floppy write protected
    By shrijeetp in forum C Programming
    Replies: 1
    Last Post: 10-03-2005, 07:00 AM
  4. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 05:48 PM
  5. Function to write string 3 times
    By Giggs in forum C++ Programming
    Replies: 15
    Last Post: 12-24-2002, 04:00 PM

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