Talk:C preprocessor
From Wikipedia, the free encyclopedia
Contents |
[edit] Pre Proccesor Abuse
I felt that the information about the abuse of pre-processor is correct. For reference check Obfuscated code and in particular check this page (on official obfuscated c code contest page) for creative pre-processor abuse.--Hq3473 21:17, 15 Apr 2005 (UTC)
- I have no problem with a section on preprocessor abuse. The problem is that everything you wrote after "For example" is incorrect. Akihabara 10:26, 16 Apr 2005 (UTC)
- All right, i agree it will probably not compile in ANSI, i will rewrite this soon with a correct example --Hq3473 16:44, 16 Apr 2005 (UTC)
- You might consider example like redefining int to long. Akihabara 23:04, 17 Apr 2005 (UTC)
- Also, note that that file is not valid C. This page shows the makefile; the compiler compiles the file after two preprocessing stages, for three in all. Valid C code only does preprocessing once. Akihabara 09:56, 15 Jun 2005 (UTC)
[edit] X-Macros
The article says, "One little-known use pattern of the C preprocessor is known by the name X-Macros." "Little-known" seems to be fairly accurate; I couldn't find any more information on the subject, anywhere. For example, a google search for "preprocessor X-Macros" finds less than 400 pages; I saw none which referred to what this article describes. Maybe this section should be removed?
Actually, rather than see it removed, I'd like to see it described in more detail; being only a moderately competent programmer myself, I couldn't tell on a cursory reading what the example code was supposed to do. I also couldn't tell if it was being presented as a useful and creative technique, or as an abuse of the preprocessor.
If the section is kept in, then a link should probably be included to at least one external website that mentions the technique. --Surelyyoujest 06:35, 25 January 2006 (UTC)
[edit] X-Macros information
The reason I wrote the X macro section is exactly because I could find so little information about it elsewhere. Whether or not this is an "abuse" is obviously a subjective opinion. The same can be said on whether it is creative. However, whether or not it is useful is not debatable, as it proved useful in many projects - and obviously allows far more applications for the DRY principle.
You said you couldn't understand what the example code was supposed to do. It simply applies the macro COMMAND multiple times, each time expanding it to something else. The same COMMAND list in the .x.h file is expanded to a command enum, to a command description list, and to a command handler list. The 3 lists are kept in sync by the x-macro, and you don't have to edit multiple files and locations to add or remove a command.
[edit] Random Note
"semicolon ... is omitted so that the macro looks 'natural' when written. It could be included in the macro definition, but then there would be lines in the code without semicolons at the end which would throw off the casual reader."
Au contraire, I think anything that weird ought to look weird, specifically in order to clue in the casual reader that the owls are not what they seem; to do otherwise creates a maintenance beartrap. If it looks like a function call, somebody may assume it's more or less equivalent to a function call expression, and make a change based on that assumption.
FWIW, many years ago I used to use the technique referred to as 'X-Macros' a lot, especially for command tables; one inclusion builds a string table, another builds a table of structs, maybe another builds a table of help messages, and having it all in one place makes it easier to maintain. The stringifying features are handy for this. However, in the last 5-10 years we have things like python which are a far better way of generating tables like that from a common file.
[edit] Examples of other uses
Krauss, do you have any more authoritiative links besides these blog and personal pages? If not, please consider moving this to "web template". These all appear to be very unusual uses for C preprocsessor, and they are all more related to web application development. Also, please note the spelling of the word "syntax" is a bit different than a non-native English speaker might expect.
On web applications, it may be used as a template engine: * C-precompiler generating HTML; * C-precompiler generating CSS; * C-precompiler generating Javascript. * CheetahTemplate use, as a subset language, the C-precompiler sintax.
Dreftymac 17:31, 9 October 2006 (UTC)
[edit] Multi-line macros?
The article currently claims: "Properly used, multi-line macros can greatly reduce the size and complexity of the source of a C program, enhancing its readability and maintainability." This seems debatable, at best. Can anyone give an example? Pfaffben 21:37, 7 March 2007 (UTC)pfaffben