Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!zaphod.mps.ohio-state.edu!van-bc!rsoft!mindlink!a563 From: a563@mindlink.UUCP (Dave Kirsch) Newsgroups: comp.os.msdos.programmer Subject: Re: #define thisneatmacro Message-ID: <3580@mindlink.UUCP> Date: 18 Oct 90 02:45:44 GMT Organization: MIND LINK! - British Columbia, Canada Lines: 71 > ctl8588@rigel.tamu.edu writes: > > Msg-ID: <9286@helios.TAMU.EDU> > Posted: 18 Oct 90 20:10:01 GMT > > Org. : TAMU > Person: LAUGHLIN, CHET > > I have a function that is called all the time by my screen handling > routines in a program. I'm not sure if TC is placing the two liner > function inline even though I've set it for speed (vs. size) optimizing. > I have been unable to write the function as a macro for all situations- > possibly somebody knows a hack? > > static void drawch(x,y,ch) > int x,y; > char ch; > { > gotoxy(x,y); > cput(ch); > } > > I tried useing the following macro, with some success... > > #define drawch(A,B,C) {gotoxy(A,B); cput(C);} > > however, the following code won't work...compiler doesn't like > the extra ; at the end of drawch... > > if (cond) > drawch(x,y,ch); > else > printf("ICK!\n"); > > Any ideas out there? Yeah, try this: #define drawch(A, B, C) (gotoxy((A), (B)), cput((C))) That should work in all cases, i.e. the if you showed will work. Because you used the braces { }, the if was terminated with a ; after the last brace. In your previous macro, it expanded to: if (cond) {gotoxy(x,y); cput(ch);}; /* NOTE extra semicolon!!! */ else printf("ICK!\n"); What happened is after the macro expanded, it leaves the semi-colon after the macro in place, so the if statement was terminated, leaving a "dangling" else statement that the compiler has no idea what to do with. With the macro I have defined to be used instead, it expands to: if (cond) (gotoxy((x), (y)), cput((ch))); else printf("ICK!\n"); Which will work correctly. I have used the comma operator to put the two function calls together, I also encased the arguments in parenthesis so there will be no problems in expansion when using "strange" arguments. -- -- /// Dave 'Zoid' Kirsch UUCP: {uunet,ubc-cs}!van-bc!rsoft!mindlink!a563 Voice: (604) 585-8844 a563@mindlink.UUCP Zoid@cc.sfu.ca Vancouver, British Columbia "In-no-sense? Nonsense!" - The Art Of Noise.