Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Small challenge: sort names Date: Wed, 08 Apr 2026 17:27:37 -0700 Organization: None to speak of Lines: 86 Message-ID: <87h5pl2d1i.fsf@example.invalid> 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 Injection-Date: Thu, 09 Apr 2026 00:27:38 +0000 (UTC) Injection-Info: dont-email.me; posting-host="6b48ea06657f6a2bb28c4f91cb64d10b"; logging-data="4189957"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bmGk/w4bTxXHAbVhrDdtv" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:9lL3kYNrEhpef2wSXGx8DXyq37k= sha1:TGUSAYnfwPmtNDh+DAWUM02x5d0= Xref: csiph.com comp.lang.c:397439 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; >> } I wouldn't, but it's a matter of taste. > Personally, I prefer to properly identify integer constants > with the appropriate type annotation. I always explicitly > check against NULL rather than using !ptr, for example. I > never use an unadorned integer for a character constant, > rather using either 'c' for printables, the legal escapes > (e.g. \n) and for any non-printable, the appropriate > octal escape. I'd probably use 0 rather than 0u for an initial value of a size_t object. 0u is probably going to have to be converted anyway (unless size_t happens to be unsigned int). I don't mind relying on the implicit conversion in `size_t count = 0;`. But for `*str != 0`, I prefer `*str != '\0'` -- not because it matters to the compiler (it doesn't), but because I find it easier to read. I like the visible reminder that it's a character value. > 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. It does. A constant 0 is always a null pointer constant. Converting an NPC to a pointer type, implicitly or explicitly, always yields a null pointer value, regardless of how null pointer values happen to be represented. But I still prefer using NULL (or nullptr in C23 or C++) when I want a null pointer value. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */