Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Thu, 13 Jun 2024 15:21:54 +0200 Organization: A noiseless patient Spider Lines: 52 Message-ID: References: <666a095a$0$952$882e4bbb@reader.netnews.com> <8734ph7qe5.fsf@nosuchdomain.example.com> <666a226d$0$951$882e4bbb@reader.netnews.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 13 Jun 2024 15:21:54 +0200 (CEST) Injection-Info: dont-email.me; posting-host="a4019a0b35be2744ae8acc392e7d37ca"; logging-data="2406480"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180IfEnzLft3oKGwfabooUlqSE/iYKzuNA=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cancel-Lock: sha1:d0ZRUmfmsZAxyFeyCoZW6LIQ6vo= In-Reply-To: <666a226d$0$951$882e4bbb@reader.netnews.com> Content-Language: en-GB Bytes: 3316 On 13/06/2024 00:34, DFS wrote: > On 6/12/2024 6:22 PM, Keith Thompson wrote: >> Janis Papanagnou writes: >>> On 12.06.2024 22:47, DFS wrote: >> [...] >>>> before: char outliers[100]; >>>> after : char outliers[100] = ""; >> [...] >>> Seriously; why do you expect [in C] a declaration to initialize that >>> stack object? (There are other languages that do initializations as >>> the language defines it, but C doesn't; it may help to learn before >>> programming in any language?) And why do you think that "" would be >>> an appropriate initialization (i.e. a single '\0' character) and not >>> all 100 elements set to '\0'? (Someone else might want to access the >>> element 'answer[99]'.) And should we pay for initializing 1000000000 >>> characters in case one declares an appropriate huge array? >> >> This: >>      char outliers[100] = ""; >> initializes all 100 elements to zero.  So does this: >>      char outliers[100] = { '\0' }; >> Any elements or members not specified in an initializer are set to zero. Yes. It's good to point that out, since people might assume that using a string literal here only initialises the bit covered by that string literal. (In C23 you can also write "char outliers[100] = {};" to get all zeros.) >> >> If you want to set an array's 0th element to 0 and not waste time >> initializing the rest, you can assign it separately: >>      char outliers[100]; >>      outliers[0] = '\0'; >> or >>      char outliers[100]; >>      strcpy(outliers, ""); >> though the overhead of the function call is likely to outweigh the >> cost of initializing the array. A good compiler will generate the same code for both cases - strcpy() is often inlined for such uses. > > Thanks.  I'll have to remember these things.  I like to use char arrays. > > The problem is I don't use C very often, so I don't develop muscle memory. > What programming language do you usually use? And why are you writing in C instead? (Or do you simply not do much programming?)