sizeof "string"? multi-line macro usage?
chris at umcp-cs.UUCP
chris at umcp-cs.UUCP
Mon Jul 14 11:04:00 AEST 1986
In article <343 at vu-vlsi.UUCP> colin at 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 at umcp-cs ARPA: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list