🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #2714

Re: #define thisneatmacro

#2714
From: Otto J. Makela
Date: Fri, 19 Oct 1990 09:55
31 lines
2098 bytes
In article <9286@helios.TAMU.EDU> ctl8588@rigel.tamu.edu (LAUGHLIN, CHET)
writes:
	[how to convert function to macro ?]
   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");

The classic solution (which perhaps should go into the FAQ posting) is to do:
#define	drawch(A,B,C)	do { gotoxy(A,B); cput(C); } while(0)
which makes the macro work correctly inside if's.  A good optimizing compiler
will remove the extra caused by 'do {} while(0)'

Of course, one thing you could also do is
#define	drawch(A,B,C)	( gotoxy(A,B), cput(C) )
(remember, a comma is the discard-value-of-1st-argument-return-value-of-2nd
operator if outside function evocations).  This is a bit more risky, however,
since things could get hairy if gotoxy or cput are also macros.

This will undoubtedly be the main topic in this newsgroup for the next three
weeks, as everyone will post their favorite answer as in the count-the-bits
and the hash function problems.
--
   /* * * Otto J. Makela <otto@jyu.fi> * * * * * * * * * * * * * * * * * * */
  /* Phone: +358 41 613 847, BBS: +358 41 211 562 (CCITT, Bell 24/12/300) */
 /* Mail: Kauppakatu 1 B 18, SF-40100 Jyvaskyla, Finland, EUROPE         */
/* * * Computers Rule 01001111 01001011 * * * * * * * * * * * * * * * * */
Technical Details
Message-ID: <OTTO.90Oct19105547@tukki.jyu.fi>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!sun-barr!olivea!mintaka!bloom-beacon!eru!hagbard!sunic!news.funet.fi!tukki.jyu.fi!jyu.fi!otto
References: <9286@helios.TAMU.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!sun-barr!olivea!mintaka!bloom-beacon!eru!hagbard!sunic!news.funet.fi!tukki.jyu.fi!jyu.fi!otto
From: otto@tukki.jyu.fi (Otto J. Makela)
Newsgroups: comp.os.msdos.programmer,comp.lang.c
Subject: Re: #define thisneatmacro
Message-ID: <OTTO.90Oct19105547@tukki.jyu.fi>
Date: 19 Oct 90 09:55:47 GMT
References: <9286@helios.TAMU.EDU>
Sender: news@tukki.jyu.fi (News articles)
Followup-To: comp.lang.c
Organization: Turing Police, Criminal AI section
Lines: 31
In-Reply-To: ctl8588@rigel.tamu.edu's message of 18 Oct 90 20:10:01 GMT