Results 1 to 5 of 5

Thread: #define

  1. #1
    Tygur
    Guest

    #define

    What is it called when you do this:
    #define whatever 1

    And what is it called when you do this:
    #define whatever(a,b) (a*b)

    Both are macros, right? Is there something I can call the first example so that it doesn't apply for the second one? Maybe a simple macro? I think I remember hearing that someplace. Also, what would the second example be called?

    I hope my question makes sense.

  2. #2
    jim mcnamara
    Guest
    You can call them macros. It is just simple substitution done by the compiler with no error checking until it tries to compile the object later on.

    YOu can change definitions like this:
    Code:
    #ifndef whatever
    #define whatever 1
    #endif
    
    to change it later on in the code:
    
    #ifdef whatever
    #undef whatever
    #endif
    #define whatever((a,b) (a*b)

  3. #3
    Tygur
    Guest
    I guess a better way of asking my question is: Is the first example one kind of macro (one that doesn't take arguments) and the second example another kind of macro (one that does take arguments), or are they both just macros?

  4. #4
    jim mcnamara
    Guest
    They are both substitutions or definitions. The name macro came later when you could pass arguments.

    eg #define zout(x) memset(&x,0x0,sizeof(x) )

    Retypes zout as the defined value and substitutes the x with whatever you supply as an argument.

    The standard C name for this is a definition, hence #define

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    in C, the first thing is usually called a "symbolic constant" because you are giving a constant (in this case 1) a symbolic name (whatever).

    The second thing is called a macro because it's a small piece of code that will be inserted instead of it's name.

    But both are simply text substitutions. In C++, you should replace all symbolic constants with real typed constants:
    const int whatever = 1;
    And macros with inline templates:
    template <class T>
    inline T whatever(T a, T b)
    {
    return a*b;
    }

    In most cases it will work exactly the same.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width