Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Bo Persson Newsgroups: comp.lang.c++ Subject: Re: Is a char array initializer a "string literal"? Date: Tue, 26 Jul 2022 11:11:42 +0200 Lines: 47 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net bqX2v/Z3I97oBT8xfiPAywur1Xg5Os72ph7AnolkeIoRNk58B1 Cancel-Lock: sha1:yxuik4oGZpo4zCkPpoIxffGNEtU= User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Content-Language: sv In-Reply-To: Xref: csiph.com comp.lang.c++:85649 On 2022-07-26 at 09:57, Juha Nieminen wrote: > In another forum I was discussing string literals and char arrays. > During the conversation this question occurred to me: > In an array initialization like this: > > char str[] = "hello"; > > can that "hello" even be considered a "string literal", or should it > be classified as something else? > > The question arose because an (actual) string literal has a type of > array-of-const-char (of a size needed to store the contents of the > string literal). So for example the type of "hello" is const char[6]. > > However, you can't initialize an array with another array. Given that > fact, that would mean that the "hello" in > > char str[] = "hello"; > > is not a const char[6] (because you can't initialize 'str' with one). > It's something else. In fact, the above is essentially just syntactic > sugar for: > > char str[] = { 'h', 'e', 'l', 'l', 'o', '\0' }; > > Thus, should that "hello", in this context, be considered an initializer > list rather than a "string literal"? > > (Yes, I wouldn't be surprised if the standard still calls it a "string > literal". But even standards are flawed and ambiguous sometimes. No > standard is absolutely perfect.) In the grammar "hello" is a string-literal (with a dash). http://eel.is/c++draft/lex.string You also have basic_string literals, with an s at the end - "hello"s. http://eel.is/c++draft/basic.string.literals Perhaps they are the real string literals? ;-) Or should we say std::string literals?