Macros Using #s

This is a discussion on Macros Using #s within the C++ Programming forums, part of the General Programming Boards category; I'm not the best with defining macros that concatenate things.... Code: #define MAC(x) #x What exactly is that supposed to ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Macros Using #s

    I'm not the best with defining macros that concatenate things....

    Code:
    #define MAC(x) #x
    What exactly is that supposed to do?

    Code:
    #define MAC(string,id) string##id
    ...Or that?

    And...aren't there rules about white space, new lines, commas, semi-colons, etc. when defining macros? Could somoene explain these a little? Isn't it that you never use semi-colons, comments, or double-quotes?

    Something like...

    Code:
    #define printf string=Hello, printf
    Would be correct?

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    When defining macros, '#' stringises it's operand. So this macro:

    [CODE]#define OUT(x) std::cout << #x << endl;[/CODE]

    Would be the same as writing

    Code:
    std::cout << "Hi" << endl;
    Assuming you were passing "Hi" to the OUT macro.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    In c++ we dont use macros.

    It's frowned upon -->

    You can easily write a function or an inline function that does the same thing.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Very true. Don't use macros, or you will be taken out the back and shot at dawn.

    See, frowned upon -- >
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    sheesh, you oop people, macros are very powerful and irreplaceable, a perfect example of a macro you would have to be crazy to migrate to an inline function is UNREFERENCED_PARAMETER()

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Oh no, a C programmer.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    strangely enough I learned on c++, then learned c and decided I wasn't all too fond of oop, I do however, keep up with oop because it's required by so many jobs

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yeah I kinda felt the same way to begin with; probably because I didn't understand some of it (polymorphism, wtf?). But now I've gotten to grips with it I think it's, well, pretty good.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    Macros aren't frowned upon by C++ people because they are non-OO. They are frowned upon because macros are easily misused and there are VERY few things that can be done with a macro for which there aren't safe alternatives.

    Ironically, that statement is applicable to C as well as to C++. But C programmers seem to religiously defend the use of macros, even when they could use safer approaches, while C++ programmers seem to religiously attempt to avoid using macros. The reality is that macros are a useful tool in both languages, but not the only tool.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    echo grumpy

    http://www.research.att.com/~bs/bs_faq2.html#macro

    <--me frowning at macros. yes, that means you, NULL.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    NULL is awesome.
    Woop?

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    NULL is void










    just kidding... DO NOT REMEBER THAT I EVER SAID THAT
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    NULL is just a big word thrown around by small men who find it easier to live in the world they’ve been given than to explore the power they have to change it.
    NULL is temporary.

    NULL is nothing.

    ... though some argue that 0 isnt nothing. Oh well.
    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
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    0 isn't nothing... I'll prove it.

    we accept these three basic principles:
    void=nothing
    NULL=0
    NULL!=void

    therefore
    0!=nothing

    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight
    NULL is temporary.
    NULL is constant, not temporary.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
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, 06:52 PM
  2. Pre-processor macros causing segfaults
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2009, 01:35 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 02:51 AM
  4. Macros vs Inline Functions
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 08-02-2007, 11:51 AM
  5. template fn replacements for msg macros
    By Ken Fitlike in forum Windows Programming
    Replies: 17
    Last Post: 10-30-2002, 06:55 AM

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