Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site hou4b.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!hogpc!houti!ariel!hou4b!ebh From: ebh@hou4b.UUCP Newsgroups: net.lang.c Subject: spaces(n) macro Message-ID: <1141@hou4b.UUCP> Date: Wed, 12-Sep-84 10:30:59 EDT Article-I.D.: hou4b.1141 Posted: Wed Sep 12 10:30:59 1984 Date-Received: Fri, 14-Sep-84 20:51:27 EDT Organization: AT&T-ISL, Holmdel, NJ Lines: 23 [send me mail if you don't see this line] from Waiming Mok: #define spaces(n) do {int i=(n);while (i--) putchar(' ');}while (0) You don't need the do .. while construct, just make a compound statement: #define spaces(n) {int i=(n);while(i--) putchar(' ');} Remember, anywhere can go, {; ...} can go. One problem can crop up: what if i is used elsewhere in the code? Imagine what spaces(i) would do. Therefore, it's wise to use some name that you never use in regular coding, like _. Also, make _ a register variable to avoid the overhead of allocating more stack space, and for the obvious speed and space advantages. Thus the final version looks like this: #define spaces(n) {int _=(n);while(_--) putchar(' ');} And yes, things like spaces(foo*2) do work. -Ed Horch {houxm,ihnp4,akgua}!hou4b!ebh p.s. On systems that support it, #define spaces(n) printf("%*s", (n), ""); works just as well.