3 ms·
> Except, of course, that doesn’t work. The preprocessor > only makes one pass through the file, meaning a macro > can’t call another macro. macro.c:
by nkurz 16y ago
> Except, of course, that doesn’t work. The preprocessor
> only makes one pass through the file, meaning a macro
> can’t call another macro.
macro.c:
#include <stdio.h>
#define LOOP(n) for (int i = 0; i < n; i++)
#define PRINT10 LOOP(10) { printf("%d", i); }
int main(void) { PRINT10; printf("\n"); }
gcc -Wall -std=gnu99 macro.c && a.out
0123456789
Of course macros can call other macros:
http://gcc.gnu.org/onlinedocs/cppinternals/Macro-Expansion.html http://gcc.gnu.org/onlinedocs/cppinternals/Macro-Expansion.h...
Ignoring his poorly protected macros, and taking nothing away from Lisp, am I missing some subtle point he's making, or is the author just flat out wrong here? I think he's just wrong, and this makes it hard for me to even read the rest of his argument.
- leppie 16y agoI noted this too. Seems the author has a broken C compiler.
- dexen 16y agoA C macro can't directly -- nor indirectly -- refer to itself; even through other macro. You can't do loops, you can't do recursion. (a nonsensical example, as I can't think of a proper one right now) #define PRINTFOO PRINTBAR #define PRINTBAR PRINTFOO PRINTFOO The ANSI C standard says a macro, while it is being expanded by the preprocessor, ``is painted blue'' (is no subject to further expansion). I'm not sure, but probably it follows the C macros are not Turing-complete. EDIT: on the other hand, a LISP macro system is a Turing-complete programming language that's geared towards manipulating lists of symbols and values (which, incidentally, is LISP program).
- nkurz 16y agoDo you have a link for the "painted blue" reference? I agree with your explanation, but had never heard that particular phrasing and was intrigued by it. But searching on Google for things like 'expand ansi macro "painted blue"' returns only your comment and a scraped spam site of someone answering a similar question.
- dexen 16y agoIndeed surprisingly hard to find a first hand reference. But http://duckduckgo.com/?q=%22painted+blue%22+expansion http://duckduckgo.com/?q=%22painted+blue%22+expansion returned link to wikipedia article, which, in turn, links to some meta-document about new C standard. There's also some post on Boost (library) mailing list: http://duckduckgo.com/?q=macro+%22painted+blue%22+boost http://duckduckgo.com/?q=macro+%22painted+blue%22+boost -> http://lists.boost.org/Archives/boost/2006/01/99264.php http://lists.boost.org/Archives/boost/2006/01/99264.php Perhaps I was wrong; perhaps the phrase was in K&R C? Can't find the book online @_@;;
- cyrus_ 16y agocorrect, not Turing-complete
- Locke1689 16y agoNote that this was, in fact, by design. Anyone who seeks to make C more complicated than this should be shot on sight.