Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Thu, 13 Jun 2024 02:19:59 +0200 Organization: A noiseless patient Spider Lines: 28 Message-ID: References: <666a095a$0$952$882e4bbb@reader.netnews.com> <8734ph7qe5.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Thu, 13 Jun 2024 02:20:01 +0200 (CEST) Injection-Info: dont-email.me; posting-host="c273a252abaa0181eac8edcfdf3add16"; logging-data="2009131"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+60Zhe0GWRhm8tHj+Ky+qS" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:HRiDg3HrvbxWM4s7o8a3Bg4fHl8= In-Reply-To: <8734ph7qe5.fsf@nosuchdomain.example.com> X-Enigmail-Draft-Status: N1110 Bytes: 2112 On 13.06.2024 00:22, Keith Thompson wrote: > > 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. Oops! This surprised me. (But you are right.) The overhead isn't [syntactically] obvious, but I'm anyway always setting a single '\0' character if I want to store strings in a 'char[]' and have it initialized to an empty string (like below). > 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. It wouldn't occur to me to use the strcpy() function, but is the function call really that expensive in C ? Janis