sizeof \"string\"? multi-line macro usage?
rbj%icst-cmr at smoke.UUCP
rbj%icst-cmr at smoke.UUCP
Tue Jul 15 10:38:21 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.
Definitely! I have seen `write(1,"Hello World\n",sizeof "Hello World\n")'.
Of course, if you want indentation, why not use a tab?
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);
You could always try a backslash after the comma, or use macros
for the arguments as well. You're right, it does sound brain damaged.
(Root Boy) Jim Cottrell <rbj at icst-cmr.arpa>
Of course, you UNDERSTAND about the PLAIDS in the SPIN CYCLE --
More information about the Comp.lang.c
mailing list