sizeof "string"? multi-line macro usage?
colin at vu-vlsi.UUCP
colin at vu-vlsi.UUCP
Sun Jul 13 04:44:03 AEST 1986
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.
Admittedly, string constants are weird because they're the only valid
array constants in the language, right? That is, they evaluate to pointers
just like other arrays do, but they magically find some memory for themselves,
whereas other arrays are only allocated memory when they're declared.
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. 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.)
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...)
Anyone know the correct answer to these questions? Thanks...
-Colin Kelley ..{cbmvax,pyrnj,psuvax1}!vu-vlsi!colin
More information about the Comp.lang.c
mailing list