const char* s1 = "abc\0", *s2 = "a\0bc";
do the expressions s1[4] and s2[4] yield the value 0, or is the behaviour
undefined? And is C99 any different from C89 in this respect?
Thanks in advance,
David Crocker
Escher Technologies
www.eschertech.com
Yes, there is an additional null element appended in all cases
(except when the string literal is used as an initializer for
an array declared with length exactly that of the literal not
counting the potential terminator).
That's as I thought. However, what then is the purpose behind footnote 24?
From the C89 standard section 3.1.4:
"In translation phase 7, a byte or code value of null is appended to each
multibyte character sequence that results from a string literal or
literals.(24)"
and footnote 24 says:
"A character string literal need not be a string literal (see 4.1.1),
because a null character may be embedded in it by a \0 escape sequence."
I can't see any reason to include footnote 24 unless there are some
situations (apart from when the string literal is used as an initializer for
an array declared with length exactly that of the literal not counting the
potential terminator) in which the compiler is not required to append a
null.
Regards - David
"a\0b" is a “character string literal” (≈ {'a', 0, 'b', 0}) but not a
“string literal” since '\0' is not allowed in middle of a string.
(Consider if strlen("a\0b") will return the right answer.)
--Joel
You slightly misquoted the footnote; it says (in the C90 standard):
A characlcr \trlnf lltcr31 need not hc 2 \trmg thee 7 I I
). because a null character may be embedded in it h:\ ;I \O c\capc
xcqucn~c
Well, not really; the PDF for the C90 standard isn't very good (I
think it was scanned from a paper copy). Rather than taking the time
to clean it up, here's what the footnote in the C99 standard says:
A character string literal need not be a string (see 7.1.1),
because a null character may be embedded in it by a \0 escape
sequence.
Note: "need not be a string", not "need not be a string literal".
> I can't see any reason to include footnote 24 unless there are some
> situations (apart from when the string literal is used as an
> initializer for an array declared with length exactly that of the
> literal not counting the potential terminator) in which the compiler
> is not required to append a null.
I think the point of the footnote is that the value of the literal *as
a whole* need not be a string. For example, given
"ab\0cd"
the stored value will be { 'a', 'b', '\0', 'c', 'd', '\0' } (ignoring
the special case Doug mentioned above). The sequence { 'a', 'b', '\0' }
is a string; the sequence as a whole is not, since a string by
definition is terminated by (and includes) the *first* null character.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Yes, that's the purpose of the footnote. It's possible (I don't recall)
that the footnote was added to address some public comment or DR.
Regards - David
"Keith Thompson" <ks...@mib.org> wrote in message
news:lnk5wu5...@nuthaus.mib.org...
It isn't entirely irrelevant. The point of appending the null character
is to create a string corresponding to the string literal; the footnote
warns that it doesn't work in all cases.
-Larry Jones
You don't get to be Mom if you can't fix everything just right. -- Calvin