Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utcs!mnetor!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP Newsgroups: net.lang.c Subject: Re: sizeof "string"? multi-line macro usage? Message-ID: <2400@umcp-cs.UUCP> Date: Sun, 13-Jul-86 21:04:00 EDT Article-I.D.: umcp-cs.2400 Posted: Sun Jul 13 21:04:00 1986 Date-Received: Sun, 13-Jul-86 23:17:13 EDT References: <343@vu-vlsi.UUCP> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 54 In article <343@vu-vlsi.UUCP> colin@vu-vlsi.UUCP (Colin Kelley) writes: >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. K&R, by the inclusion of an explanatory sentence, made it ambiguous. Appendix A, \S 7.1, Primary expressions, p. 186: A string is a primary expression. Its type is originally ``array of char''; but following the same rule given above for identifiers, this is modified to ``pointer to char'' and the result is a pointer to the first character in the string. (There is an exception in certain initializers; see \S 8.6.) Had they left out the note about conversion to `pointer to char', or had they mentioned sizeof in that last sentence, it would not be ambiguous. The ANSI draft standard simply declares a string constant to have the type `array of char', making `sizeof "foo the bar"' definitely equal to 12*sizeof(char). >... 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(' '); There is another reason to avoid this: each time you mention the string, most compilers generate another instance of it. Using >char prompt[] = PROMPT; not only >... _does_ work in Regulus but also may keep your data space usage lower. * * * >Does ANSI say anything about macro usage spanning more than one line? The way I read the draft standard, >#define max(a,b) ((a > b) ? a : b) > >x = min(very-long-expression-here, > another-very-long-expression-here); is legal; but this is not explicit. It depends on allowing `a sequence of tokens' to include a newline. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516) UUCP: seismo!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@mimsy.umd.edu