Macros exist in many programming languages, for example, C.

  1. A data type is a set of values with operations on the values. Are macros values, or data types?
  2. Are macros identifiers, variables, or constants?
  3. Are macros something not belonging to a programming language, and something that will be translated to the programming language in the preprocessing step of compilation?


closed as too broad by gnat, user40980, Ampt, Dan Pichelman, ratchet freak Sep 16 '14 at 9:04

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


macros have a very different meaning in C (or C++) than e.g. in Common Lisp or Scheme (or some homoiconic programming language).

In particular, in Lisp or Scheme you can have macros with some local binding scope. In C or C++ the scope is textual, always from the #define till the end of translation unit or some #undef. And C/C++ macros cannot define other macros (but you could do that in Lisp)

In C and C++, the preprocessor is the first phase of the compiler, and operates at a textual level (in particular, the C/C++ preprocessor does not see any abstract syntax tree, only lexical tokens). The compiler proper sees only preprocessed form (so your point 3 is true, and your points 1 and 2 become irrelevant). So macros are living in a different world than the values or variables. In particular, macros could be defined without even being a well formed expression or instruction, like in

int counter;
/// UGLY!
#define MY_BEGIN { counter++;
#define MY_END(N) N--; }
void myfun(void)
  MY_BEGIN printf("counter=%d\n", counter); MY_END(counter)

But I feel that the above code is ugly!

A C or C++ compiler gives you the ability to get the preprocessed form. With GCC use e.g. gcc -C -E foo.c > foo.i to get into file foo.i (which you could inspect with some editor or pager) the preprocessed form of source file foo.c ... and read the documentation of cpp, i.e. of GCC preprocessor.

Notice that C/C++ macros are not able to generate easily any kind of code. For example, you cannot code parser generators like bison or ANTLR with only C/C++ macros (you need some external C code generating tool like bison). C++ has templates, but you cannot for instance easily code a printer generator (i.e. print all fields of some struct given as a template argument) or a serializer generator with them. Such things are possible in Lisp.

In Common Lisp, macros are homogeneous with the programming language, and are values. the evaluation and compilation process is very different from C. For example, with SBCL implementation of Common Lisp, the evaluator and compiler is part of every application, and applications in Lisp routinely build some Lisp expressions at runtime then compile them (at runtime also). So the code itself is changing at runtime!

Very grossly speaking, in Lisp macros are some kind of value (i.e. is a datatype); in Scheme or MELT (a domain specific language to extend and customize GCC), macro definitions introduce some kind of binding. (But things are in fact more subtle, so this is a simplification and is somehow "wrong"). Read about quasi-quotations.

C.Queinnec's book Lisp In Small Pieces explains that quite well. Scott's book Programming Language Pragmatics is also an interesting reading.

  • Thanks. Are they values of what data type in CL? – Tim Sep 12 '14 at 4:51
  • Grossly speaking, of some macro datatype. – Basile Starynkevitch Sep 12 '14 at 4:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.