Path: utzoo!utgpu!watserv1!watmath!iuvax!mailrus!ncar!boulder!ruede From: ruede@boulder.Colorado.EDU (Ulrich Ruede) Newsgroups: comp.lang.c Subject: Re: Personal dialects and C++ overloading Summary: Macros for customizing C Message-ID: <16477@boulder.Colorado.EDU> Date: 2 Feb 90 02:36:37 GMT References: <5940014@hpcupt1.HP.COM> <33889@ucbvax.BERKELEY.EDU> <1922@gmdzi.UUCP> < <1990Jan29.210357.366@eng.umd.edu> <90029.194640JI8@PSUVM.BITNET> <4152@helios.TAMU.EDU> <33478@news.Think.COM> <4156@helios.TAMU.EDU> Sender: news@boulder.Colorado.EDU Reply-To: ruede@boulder.Colorado.EDU (Ulrich Ruede) Organization: University of Colorado, Boulder Lines: 72 In article <4156@helios.TAMU.EDU> john@stat.tamu.edu (John S. Price) writes: > >#define FORNEXT(x,y,x) for (x=y;x >... >FORNEXT(x,0,10); > >That is not standard C, and I feel that using this type of macro is >bad coding style. > Well, knowing that the following program has to do with a graph (with attributed nodes and edges) would you prefer to see an initialization like FOR_ALL_NODES_OF(a_mesh, this_node) FOR_ALL_EDGES_OF(this_node, this_edge) this_edge->component= 0; END_ALL_EDGES END_ALL_NODES or rather { Node_Ptr this_node; for(this_node= (a_mesh)->grd; (this_node) != NULL; (this_node)= (this_node)->next){ { register Edge_Ptr this_edge= (this_node)->c, _end= (this_edge)+((this_node)->num_edges); for(; (this_edge)<_end; (this_edge)++){ this_edge->component= 0; } } } } This has been produced by cb on the expanded result. The macros enable me to define the data structures and the access to these structures together, as one module. If I don't use macros, the information how to access all nodes of the graph is dispersed throughout the program. Changing the basic structure then means to rewrite everything. If I have the macros, I need to change just the macro definition. I am using m4, so everyone who prefers the expanded result, can easily get it with all symbolic constants still in symbolic form. I also think that changing the syntax of a language is not a bad thing by itself. If you write a function, you have also changed the syntax of the language by introducing a new terminal symbol at the expression level. In some sense all programming consists in changing the syntax of a given base language, until you have a *specialized language* in which you solve your problem with a single statement like work(); This view of programming may be more obvious users of languages like Prolog or Forth. I agree, that only because you dislike C syntax you shouldn't do things like #define BEGIN { (if you can't see the braces, get a better terminal (:-) or #define OR || just as you wouldn't introduce new names for printf and other standard library functions. Writing your own subroutines, however, makes perfect sense, just as I believe it makes sense to use macros as mine above. It would still be better if these capabiltities were built into some language (without sacrificing performance). Ulrich Ruede ruede@boulder.colorado.edu (until Friday) ruede@infovax.informatik.tu-muenchen.dbp.de (next week)