Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Small challenge: sort names Date: Wed, 08 Apr 2026 22:14:25 -0700 Organization: A noiseless patient Spider Lines: 92 Message-ID: <86h5pkww9a.fsf@linuxsc.com> References: <10r4h6o$396qn$1@dont-email.me> <10r537m$in2o$4@dont-email.me> <10r5vh2$3mo17$2@dont-email.me> <10r64uq$3pf3r$1@dont-email.me> <20260409001030.000006c9@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Thu, 09 Apr 2026 05:14:27 +0000 (UTC) Injection-Info: dont-email.me; posting-host="58cb476bd973d15d025d3b0e2ffe71fe"; logging-data="118640"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18jhHHMc+kTqjnVACIRuwA1zMcLwd4QUQ0=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:6ru7V2fThp0bqdzqVnlaFn5QLJk= sha1:qmxgAiRpTDuwzJjdupS67gkRkQA= Xref: csiph.com comp.lang.c:397444 scott@slp53.sl.home (Scott Lurndal) writes: > Michael S writes: > >> On Wed, 08 Apr 2026 21:04:56 GMT >> scott@slp53.sl.home (Scott Lurndal) wrote: >> >>> Bart writes: >>> >>>> On 08/04/2026 17:25, DFS wrote: >>>> >>>>> On 4/8/2026 4:22 AM, Janis Papanagnou wrote: >>>> >>>> //count characters in a string >>>> int countchr(char *str, char chr) >>>> {int >>>> c=0,cnt=0;while(str[c]!='\0'){if(str[c]==chr){cnt++;}c++;}return >>>> cnt;} >>>> >>>> I couldn't quite figure out what it did; counting characters in a >>>> string (like the comment says)? But strlen will do that. Then I >>>> noticed that the extra 'chr' parameter, so maybe it stops at 'chr'. >>>> >>>> But when I tried running it as countchr("ABCDEFGH", 'E'), it >>>> returned 1, not 4 or 5. >>>> >>>> So I refactored it like this: >>>> >>>> int countchr(char *str, char chr) { >>>> int c=0, cnt=0; >>>> while (str[c]!='\0') { >>>> if (str[c] == chr) cnt++; >>>> c++; >>>> } >>>> return cnt; >>>> } >>> >>> Idiomatically, I might have written >>> >>> size_t >>> countchar(const char *str, char match) >>> { >>> size_t count = 0u; >>> for(; *str != '\0'; str++) count += (*str == match); >>> return count; >>> } >> >> I'd change it to >> >> size_t >> countchar(const char *str, char match) >> { >> size_t count = 0; >> for(; *str != 0; str++) count += (*str == match); >> return count; >> } > > Personally, I prefer to properly identify integer constants > with the appropriate type annotation. A dangerous habit if followed out of habit. For example: size_t ones = -1; // makes 'ones' be all one bits size_t ones = -1u; // oops! can be 32 ones rather than 64 > I always explicitly check against NULL rather than using !ptr, > for example. I consider the treatment of null pointers and the way if(), for(), while(), etc, work to be a pleasing symmetry and an elegant simplifying principle of the C language. Pointers are a special kind of multi-valued booleans. Writing this char *p = ...; if( p != NULL ) ... is just as repugnant as this _Bool b = ...; if( b != false ) ... > Perhaps some of this is pedantic. I worked with a system > where the null pointer constant was not the value zero, so > I'm aware that !ptr may not actually do the right thing in > such cases. The semantics of !ptr has been codified to mean 1 for null pointers since the original C standard. Heck, even during the 1970s when C allowed assigning integers directly to pointers, K&R said that assigning 0 to a pointer produces a pointer guaranteed to be different than a pointer to any object. Any C implementation where !ptr does the wrong thing should be avoided like the plague.