sizeof "string"? multi-line macro usage?
Gregory Smith
greg at utcsri.UUCP
Mon Jul 14 04:53:45 AEST 1986
In article <343 at vu-vlsi.UUCP> colin at 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
More information about the Comp.lang.c
mailing list