Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: C String terminator Date: Tue, 21 Jan 2020 10:41:17 -0800 Organization: None to speak of Lines: 35 Message-ID: <87d0bc8xf6.fsf@nosuchdomain.example.com> References: <87muaoimkt.fsf@nosuchdomain.example.com> <87a76ofad3.fsf@bsb.me.uk> <87muaodqqn.fsf@bsb.me.uk> <87zhencyud.fsf@bsb.me.uk> <1by2u7s456.fsf@pfeifferfamily.net> <87o8v3cg0i.fsf@bsb.me.uk> <877e1rc6f7.fsf@bsb.me.uk> <87v9p9bylk.fsf@bsb.me.uk> <861rrtj9hx.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="c0a39475b9d9d4706572b5fe25f2da35"; logging-data="1580"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XlxDQi5CLyFPCvd0bMwqa" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cancel-Lock: sha1:eKhH+drIfV9LY4m7N0GfIiJx0gU= sha1:86yAshpoQmJuekGOvrz2394avnk= Xref: csiph.com comp.lang.c:149318 Tim Rentsch writes: > Lew Pitcher writes: [...] >> Correction #1: in this case, the critical point is that, here, printf() >> expects a C "string" as it's second argument (as well as the first argument, >> which is the given format string "[%s]\n"). > > Technically, that isn't right. A %s conversion specifier requires > as its argument a pointer to the first element of an array of > character type, not a pointer to a string. It is true in this > particular case that the array must have a null character in it to > avoid undefined behavior, but the argument description says "pointer > to array of character type", and does not mention "string". > > (Also: the possessive of "it" is "its", with no apostrophe. The > word "it's", with an apostrophe, is a contraction of "it is".) I believe the reason it's described that way is that %s with a field width can require a pointer that isn't necessarily a pointer to a string. For example, this is well defined: char not_a_string[3] = "foo"; printf("%.3s\n", not_a_string); The standard *could* have equivalently stated that a pointer to a string is required when the format contains a "%s" directive, but that would have made it overly wordy with no clear benefit. A pointer to a string *is* required for a "%s" directive, but the standard says so only indirectly. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com [Note updated email address] Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */