Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site utcsri.UUCP Path: utzoo!utcsri!greg From: greg@utcsri.UUCP (Gregory Smith) Newsgroups: net.lang.c Subject: Re: sizeof "string"? multi-line macro usage? Message-ID: <3098@utcsri.UUCP> Date: Sun, 13-Jul-86 14:53:45 EDT Article-I.D.: utcsri.3098 Posted: Sun Jul 13 14:53:45 1986 Date-Received: Sun, 13-Jul-86 15:03:09 EDT References: <343@vu-vlsi.UUCP> Reply-To: greg@utcsri.UUCP (Gregory Smith) Organization: CSRI, University of Toronto Lines: 65 Summary: In article <343@vu-vlsi.UUCP> colin@vu-vlsi.UUCP writes: >Question 1. > >Is it (portably) legal for sizeof to operate on a string constant? I >checked K&R, and they seem to have nothing to say about the subject. .. >This came up because I have coded > >#define PROMPT "prog> " > >in an include file, and then later used > >for (i = 0; i < sizeof(PROMPT) - 1; i++) /* indent to first char of input */ > putchar(' '); > >to get the proper indentation for error messages. This worked great when >compiled on Pyramid C, VAX VMS C, and Lattice C on a PC. Then I ran into the >Regulus C compiler (Regulus 4.2C) which is truly brain-damaged in many >respects; as far as I can tell, its sizeof returns a random value when given >a string. Regulus is screwed up. BTW, printf( "%*s",sizeof(PROMPT)-1, " "); will work except that Regulus may not support * in formats. Or if you want to get really efficient at the expense of weirdness: printf( " "+20-( sizeof(PROMPT)-1 ) ); >So is it just Regulus that's screwed up, or do I have to explicitly >declare > >char prompt[] = PROMPT; > >and then take sizeof(prompt)? (The latter _does_ work in Regulus.) > Interestingly, using PCC, sizeof("foobar") gives 7 but stuffs the string "foobar" into memory which will never be used, so you may as well use the prompt[] solution above. This may depend on the dialect of PCC you are using, but all of ours do, and so does cc11 (PDP11). Dem bugs, dem bugs.... > >Question 2. > >Does ANSI say anything about macro usage spanning more than one line? Lattice >C has something like > >#define max(a,b) ((a > b) ? a : b) > >but then barfs when I use the macro over more than one line, like > >x = min(very-long-expression-here, > another-very-long-expression-here); > >All the other compilers I've used have no trouble with this. (By not allowing >multi-line macro usage, Lattice is screwing with the usually transparent >interchangeability of functions and macros...) > This is #definitely a preprocessor bug. -- "You'll need more than a Tylenol if you don't tell me where my father is!" - The Ice Pirates ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg