Are macros values, data types, identifiers, variables, constants, or …? [closed]

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?

Thanks,

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=%dn", 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.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật