Thread: what is macro

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1

    what is macro

    What are macro in c and how they are defined

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Macros are a preprocessor directive that replaces the macro name with the code in the definition before compilation.
    Code:
    #include <stdio>
    
    #define myMacro printf("Hello World")
    
    int main() {
       myMacro;
    
       return 0;
    }
    There's about a dozen and a half reasons not to use macros, but that's how they are defined if you wished to use them. Consider using inline functions instead.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Quote Originally Posted by SlyMaelstrom
    Consider using inline functions instead.
    Hellow !!! this is a c forum not c++.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    You can inline functions in C, genius.




    (Right?)
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, that's correct, my bad. Either way, avoid macros.

    Quote Originally Posted by ahluka
    You can inline functions in C, genius.
    (Right?)
    No, I don't believe it's standard. Some compilers will take it, but I think it just ignores the keyword when you compile a C source.

    EDIT: Off of a little research it seems inlining was fully accepted into the C99 standard, so it may actually be applied in C code. I'm not positive, though.
    Last edited by SlyMaelstrom; 05-02-2006 at 08:05 AM.
    Sent from my iPadŽ

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ok I didn't know that.

    EDIT: You must be able to. I swear I've done it before. Unless I tricked myself into thinking I was using C. Hmm.

    EDIT2: You have to stop editing at the same time as me.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    c99 does support function inlining. Unfortunately, many people use microsoft compilers which do not handle c99.

    Gcc does, though - just compile with -std=c99.
    Last edited by Ken Fitlike; 05-02-2006 at 08:30 AM. Reason: typo
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    #include <stdio.h>
    
    #define SIX  1+5
    #define NINE 8+1
    
    int main ()
    {
        printf("%d times %d equals %d", SIX, NINE, SIX*NINE);
        return 0;
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("%d times %d equals %d", SIX, NINE, SIX*NINE);
    I wonder what this will print.
    A demonstration of the precedence traps which can happen with macros no doubt
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM