Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!amdcad!ames!ptsfa!ihnp4!homxb!genesis!hotlg!anumb!eao From: eao@anumb.UUCP (e.a.olson) Newsgroups: comp.lang.c Subject: Re: #if inside #define -- easy to do, but is it a good idea? Message-ID: <125@anumb.UUCP> Date: Mon, 28-Sep-87 22:11:04 EDT Article-I.D.: anumb.125 Posted: Mon Sep 28 22:11:04 1987 Date-Received: Thu, 1-Oct-87 02:21:21 EDT References: <157@decvax.UUCP> Organization: AT&T Bell Labs, Andover MA Lines: 37 > From time immemorial, C has forbidden # control lines inside a > macro definition. When a collegue begged for this capability, > I discovered it was trivial to add it to Decus CPP (which was posted > to Usenet mod.sources about 2-3 years ago). It might be used > as follows: > #define set_bit(bit) \ > #if debug \ > printf("setting %04x\n", bit), \ > #endif \ > (device_register = (bit)) > > (and it does violate the formal model), but it might be worth thinking > of for the future. > > Or, did I forget something obvious? > The point is that, as you said, your programs are non-portable. You could kludge it with some definition like below or even better make DEBUG a varargs function that compiles into something that prints out the args when DEBUG is compiled with debug and does nothing otherwise (except then you'd lose the inline code speed ). Matter of fact, this is probably pretty bad. BUT DON'T CHANGE THE COMPILER !!! #if debug # define DEBUG(format, arg) printf(format, arg) #else # define DEBUG(format, arg) 0 #endif #define set_bit(bit) \ DEBUG("setting %04x\n", bit), \ (device_Register = (bit))